湘潭市赛 Josephus Problem 线段树
2012-07-22 23:43
471 查看
http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1149
Josephus ProblemDo you know the famous Josephus Problem? There are n people standing in a circle waiting to be executed. The counting out begins at the firstpeople in the circle and proceeds around the circle in the counterclockwise direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. In traditional Josephus Problem, the number of people skipped in each round is fixed, so it's easy to find the people executed in the i-th round. However, in this problem, the number of people skipped in each round is generated by a pseudorandom number generator: x[i+1] = (x[i] * A + B) % M. Can you still find the people executed in the i-th round? Input There are multiple test cases. The first line of each test cases contains six integers 2 ≤ n ≤ 100000, 0 ≤ m ≤ 100000, 0 ≤ x[1], A, B < M ≤ 100000. The second line contains m integers 1 ≤ q[i] < n. Output For each test case, output a line containing m integers, the people executed in the q[i]-th round. Sample Input 2 1 0 1 2 3 1 41 5 1 1 0 2 1 2 3 4 40 Sample Output 1 2 4 6 8 35 #include<iostream> #include<stdio.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 int ans[100001]; int xw[100001]; const int maxn=200000; int w,sum[maxn<<2]; void build(int l,int r,int rt){///建立线段树 sum[rt] = r - l + 1; if(l == r) return ; int m = (l+r) >> 1; build(lson); build(rson); } int update(int p,int l,int r,int rt){///更新单个节点 sum[rt]--; if(l == r) return l ; int m = (l + r) >> 1; if(p <= sum[rt<<1]) return update(p,lson); else return update(p-sum[rt<<1],rson); } int main() { int n,m,i; int x,A,B,M; while(scanf("%d%d%d%d%d%d",&n,&m,&x,&A,&B,&M)!=EOF) { build(1,n,1); int z = 1; for(i = 1; i <= n; i++) { z = ((int)x+z)%sum[1]; if(z == 0) z = sum[1]; int s = update(z,1,n,1); ans[i] = s; x = (int)(((__int64)x * A + B) % M); } for(i = 0; i < m; i++) scanf("%d",&xw[i]); for(i=0;i<m-1;i++) printf("%d ",ans[xw[i]]); if(m!=0) printf("%d",ans[xw[m-1]]); printf("\n"); } return 0; } |
相关文章推荐
- 湘潭市赛 Josephus Problem 线段树
- 湘潭市赛 Josephus Problem 线段树
- 湘潭市赛 Josephus Problem 线段树
- 湘潭市赛 Josephus Problem 线段树
- 湘潭市赛 Josephus Problem 线段树
- 湘潭市赛 Josephus Problem 线段树
- 湘潭市赛 Josephus Problem 线段树
- CDOJ_1591_An easy problem A(线段树水题)
- POJ 3468 A Simple Problem with Integers(线段树区间更新模板题)
- poj3468:A Simple Problem with Integers(线段树lazy)
- poj 3468 A Simple Problem with Integers(线段树,成段更新)
- CodeForces 276E - Little Girl and Problem on Trees 区间更新..N+1个线段树
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
- HDU-5475 An easy problem(模拟||(倒着计算+线段树))
- POJ-3468-A Simple Problem with Integers(线段树区间维护 重写Lazy)
- hdu 5475 An easy problem 线段树
- hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
- hdu - 5475 - An easy problem - (线段树)
- A Simple Problem with Integers(线段树 成段更新)