Wannafly挑战赛15-C-出队

链接:https://www.nowcoder.com/acm/contest/112/C
来源:牛客网

约瑟夫问题(https://baike.baidu.com/item/约瑟夫问题),n个人,1 2报数 1出队( 就是体育课的时候1 2报数 1出队,2留下),q次询问,每次求第x个人是第几个出队的

输入描述:

第一行两个数n,q
接下来q行,每行一个数x,表示询问

输出描述:

一行输出一个询问的答案

示例1

输入

4 3
2
3
4

输出

3
2
4

说明

1 2 3 4围成一圈,第一轮:1 2报数,1出队,2留下,3出队,4留下,第二轮,2出队,4留下

备注:

q≤500000
n和x≤1e18

    我们对剩下的元素接在末尾继续编号显然奇偶性不受影响只要知道下一次的编号就好了,如果是偶数就继续查找下一个编号否则退出,答案就是
(编号+1)/2;
  
 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 #include<cstdio>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<cmath>
 9 #include<ctime>
10 #include<time.h> 
11 #include<algorithm>
12 using namespace std;
13 #define mp make_pair
14 #define pb push_back
15 #define debug puts("debug")
16 #define LL unsigned long long 
17 #define pii pair<int,int>
18 #define eps 1e-10
19 #define inf 0x3f3f3f3f
20 
21 vector<LL>v;
22 LL pre[1103]={0};
23 int main()
24 {
25     LL n,m,i,j,k,q,x,ans,tot=0;
26     scanf("%lld%lld",&n,&q);
27     
28     while(q--){
29         scanf("%lld",&x);
30         if(x%2==1){
31             ans=(x+1)/2;
32         }
33         else{
34             LL delta=n-(x/2);
35             while(x%2==0){
36                 x=x+delta;
37                 delta/=2;
38             }
39             ans=(x+1)/2;
40         }
41         printf("%lld\n",ans);
42     }
43     return 0; 
44 }
45 /*
46 
47 */

猜你喜欢

转载自www.cnblogs.com/zzqc/p/9028116.html