War Games 2

Time limit: 1.0 second
Memory limit: 64 MB

Background

During the latest war games (this story is fully described in the problem "War games") the Minister of Defense of the Soviet Federation comrade Ivanov had a good chance to make sure personally, that an alertness of the Soviet Army under his command is just brilliant. But there was a thing, that continued to worry him. Being an outstanding commander, he realized, that only physical conditions of the soldiers were demonstrated. So the time came to organize one more war games and examine their mental capacity.
General Rascal was appointed to be responsible for the war games again. The general donated the allocated funds to the poor and went to bed free-hearted. In his dream, the tactics manual appeared to him and described a scheme, that allows to organize the war games absolutely free of charge.

Problem

In accordance with this scheme, the war games are divided into N phases; and N soldiers, successively numbered from 1 to N, are marching round a circle one after another, i.e. the first follows the second, the second follows the third, ..., the (N-1)-th follows the N-th, and the N-th follows the first. At each phase, a single soldier leaves the circle and goes to clean the WC, while the others continue to march. At some phase, the circle is left by a soldier, who is marching Kpositions before the one, who left the circle at the previous phase. A soldier, whose number is K, leaves the circle at the first phase.
Surely, Mr. Rascal cherished no hope about his soldiers' abilities to determine an order of leaving the circle. "These fools can not even paint the grass properly", - he sniffed scornfully and went to sergeant Filcher for an assistance.

Input

The only line contains the integer numbers N (1 ≤ N ≤ 100000) and K (1 ≤ K ≤ N).

Output

You should output the numbers of soldiers as they leave the circle. The numbers should be separated by single spaces.

Sample

input output
5 3
3 1 5 2 4

分析:约瑟夫出环顺序,树状数组;

   每次出环是第几个是知道的,所以只需二分求出在原环上的标号即可;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
const int dis[][]={{,},{-,},{,-},{,}};
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,a[maxn],now;
void add(int x,int y)
{
for(int i=x;i<=n;i+=(i&(-i)))
a[i]+=y;
}
int get(int x)
{
int ans=;
for(int i=x;i;i-=(i&(-i)))
ans+=a[i];
return ans;
}
int main()
{
int i,j;
scanf("%d%d",&n,&k);
rep(i,,n)add(i,);
for(i=n;i>=;i--)
{
now=(now+k-)%i;
int l=,r=n,ans;
while(l<=r)
{
int mid=l+r>>;
if(get(mid)>=now+)ans=mid,r=mid-;
else l=mid+;
}
add(ans,-);
printf("%d ",ans);
}
//system("Pause");
return ;
}

ural1521 War Games 2的更多相关文章

  1. 战争游戏(War Games 1983)剧情

    战争游戏 War Games(1983) 人工控制导弹发射 傍晚大雾,两值工作人员自驾一辆轿车到达监控俄罗斯核战争的防空基地,在门口出示工作证后进入基地,两工作人员和同事换班后,进入防空系统控制室开始 ...

  2. 1521. War Games 2(线段树解约瑟夫)

    1521 根据区间和 来确定第k个数在哪 #include <iostream> #include<cstdio> #include<cstring> #inclu ...

  3. hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

    #1392 : War Chess 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Rainbow loves to play kinds of War Chess gam ...

  4. ARTIFICIAL INTELLIGENCE FOR GAMES (Ian Millington / John Funge 著)

    相关网站:http://www.ai4g.com PART I AI AND GAMESCHAPTER1 INTRODUCTIONCHAPTER2 GAME AIPART II TECHNIQUESC ...

  5. Google Code Jam 2014 资格赛:Problem D. Deceitful War

    This problem is the hardest problem to understand in this round. If you are new to Code Jam, you sho ...

  6. Wind Simulation in 'God of War'(GDC2019 战神4风力场模拟)

    Wind Simulation in 'God of War'(GDC2019) 战神4中的风力场模拟 这次带来的分享的主题是,圣莫妮卡工作室他们在战神4中关于GPU模拟风力场. 演讲者Rupert ...

  7. poj 1085 Triangle War (状压+记忆化搜索)

    Triangle War Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2685   Accepted: 1061 Desc ...

  8. 用Maven部署war包到远程Tomcat服务器

    过去我们发布一个Java Web程序通常的做法就是把它打成一个war包,然后用SSH这样的工具把它上传到服务器,并放到相应的目录里,让Tomcat自动去解包,完成部署. 很显然,这样做不够方便,且我们 ...

  9. 多War项目中静态文件的共享方案

    [原创申明:文章为原创,欢迎非盈利性转载,但转载必须注明来源] 在互联网产品中,一般会有多个项目(Jar.WAR)组成一个产品线.这些WAR项目,因为使用相同的前端架构(jQuery.easyui等) ...

随机推荐

  1. [科普]DNS相关的攻击介绍

    一  什么是DNS DNS 是域名系统 (Domain Name System) 的缩写,是因特网的一项核心服务,它作为可以将域名和IP地址相互映射的一个分布式数据库,能够使人更方便的访问互联网,而不 ...

  2. project文件问题

    到编译文件中看看,红色的删除,坤哥这样做的 .

  3. MySQL源码安装(centos)

    1.去MySQL官网下载源码包 地址:http://dev.mysql.com/downloads/mysql/ 下载完后需要检查文件的MD5,以确认是否从官网下载的原版本(以防被人篡改过该软件) 使 ...

  4. 响应式web设计的优化

    在响应式网页的设计过程中,其中也会涉及到一些用户体验方面的问题和弊端,需要进一步的优化. (1)减轻Javascript库负载 对于移动端来说,jQuery表现的太过厚重,而现在针对移动端的状态来说, ...

  5. div.2/Bellovin<最长上升子序列>

    题意: 序列arr[i--n];输出以a[i]为结尾的最长上升子序列.1<=n<=100000; 思路: O(n*log(n)),求最长上升子序列. #include<cstdio& ...

  6. UVALive 2517 Moving Object Recognition(模拟)

    题目看上去很吓人,很高端,但其实很简单,不要被吓到,照搬题目的公式就可以了. 方法:用BFS求出最大块和重心,找出题目公式需要的未知量,然后套到题目公式里就可以求出答案了. 代码: #include& ...

  7. Git学习 -- 新建版本库

    创建目录 mkdir git cd git 2 初始化git init #会在目录下自动创建一个.git目录,用于跟踪管理版本库,不要修改 3 向版本库中添加文件git add readme.txt ...

  8. Light OJ - 1058 Parallelogram Counting(判定平行四边形)

    Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...

  9. context-param与init-param的区别与作用

    <context-param>的作用: web.xml的配置中<context-param>配置作用 1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件 ...

  10. PAT1014

    Suppose a bank has N windows open for service. 一个银行有N个服务的窗口 There is a yellow line in front of the w ...