1、http://poj.org/problem?id=2886
2、题目大意:
N 个小孩围成一圈,他们被顺时针编号为 1 到 N。每个小孩手中有一个卡片,上面有一个非 0 的数字,游戏从第 K 个小孩开始,他告诉其他小孩他卡片上的数字并离开这个圈,他卡片上的数字 A 表明了下一个离开的小孩,如果 A 是大于 0 的,则下个离开的是左手边第 A 个,如果是小于 0 的,则是右手边的第 -A 个小孩。游戏将直到所有小孩都离开,在游戏中,第 p 个离开的小孩将得到 F(p) 个糖果,F(p) 是 p 的约数的个数,问谁将得到最多的糖果。输出最幸运的小孩的名字和他可以得到的糖果。
题目:
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 8763 | Accepted: 2666 | |
Case Time Limit: 2000MS |
Description
N children are sitting in a circle to play a game.
The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the others the integer on his card and jumps out of the circle. The integer on his card tells the next child to jump out. Let A denote the integer. If A is positive, the next child will be the A-th child to the left. If A is negative, the next child will be the (−A)-th child to the right.
The game lasts until all children have jumped out of the circle. During the game, the p-th child jumping out will get F(p) candies where F(p) is the number of positive integers that perfectly divide p. Who gets the most candies?
Input
Output
Output one line for each test case containing the name of the luckiest child and the number of candies he/she gets. If ties occur, always choose the child who jumps out of the circle first.
Sample Input
4 2 Tom 2 Jack 4 Mary -1 Sam 1
Sample Output
Sam 3
Source
3、AC代码:
#include<stdio.h> #include<algorithm> using namespace std; #define N 500005 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 int num[N],sum[N*4]; char str[N][15]; int f[N],cur_i,next_i,cur_n,ans,ans1,cnt,cur; void pushUp(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void build(int l,int r,int rt) { if(l==r) { sum[rt]=1; return ; } int m=(l+r)>>1; build(lson); build(rson); pushUp(rt); } void update(int p,int l,int r,int rt) { if(l==r) { sum[rt]--; next_i=num[l]; cur_n--; if(ans<f[cnt]) { ans=f[cnt]; ans1=l; } return ; } int m=(l+r)>>1; if(p<=sum[rt<<1]) update(p,lson); else update(p-sum[rt<<1],rson); pushUp(rt); } int main() { int n,k; for(int i=1; i<=N; i++) { for(int j=1; i*j<=N; j++) { f[i*j]++; } } while(scanf("%d%d",&n,&k)!=EOF) { build(1,n,1); for(int i=1; i<=n; i++) { scanf("%s%d",str[i],&num[i]); } cur_n=n; cur_i=n; next_i=k; cnt=1; ans=0; ans1=1; for(int i=1; i<=n; i++) { if(next_i>=0) cur_i--; cur=(cur_i+next_i%cur_n+cur_n)%cur_n; update(cur+1,1,n,1); cur_i=cur; cnt++; } printf("%s %d\n",str[ans1],ans); } return 0; }