[POJ 2886] Who Gets the Most Candies?
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
1 /* 2 挑战2-线段树 3 反素数+线段树(删除编号为k的元素) 4 by-solution 5 */ 6 #include<cstdio> 7 #include<cstdlib> 8 #include<cstring> 9 #include<iostream> 10 #include<cmath> 11 #include<algorithm> 12 #define ll long long 13 #define ls x<<1 14 #define rs x<<1|1 15 using namespace std; 16 17 const int N = 500010; 18 19 int n,k,ans,tr[N*4]; 20 int a[40]={1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,10080,15120,20160,25200,27720,45360,50400,55440,83160,110880,166320,221760,277200,332640,498960,500001},b[40] = {1,2,3,4,6,8,9,10,12,16,18,20,24,30,32,36,40,48,60,64,72,80,84,90,96,100,108,120,128,144,160,168,180,192,200,1314521}; 21 22 struct Node { 23 int v; 24 char s[11]; 25 }q[N]; 26 27 int gi() { 28 int x=0,o=1; char ch=getchar(); 29 while(ch!='-' && (ch<'0'||ch>'9')) ch=getchar(); 30 if(ch=='-') ch=getchar(),o=-1; 31 while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar(); 32 return o*x; 33 } 34 35 void build(int x, int l, int r) { 36 tr[x]=r-l+1; 37 if(l==r) return; 38 int mid=(l+r)>>1; 39 build(ls,l,mid),build(rs,mid+1,r); 40 } 41 42 int query(int x, int l, int r, int k) { 43 tr[x]--; 44 if(l==r) return l; 45 int mid=(l+r)>>1; 46 if(k<=tr[ls]) return query(ls,l,mid,k); 47 else return query(rs,mid+1,r,k-tr[ls]); 48 } 49 50 int main() {//poj2886 51 while(~scanf("%d%d", &n, &k)) { 52 int cnt=0,p,pos,i,n1=n; 53 for(i=1; i<=n; i++) { 54 scanf("%s%d", q[i].s, &q[i].v); 55 } 56 build(1,1,n); 57 while(a[cnt]<=n) cnt++; 58 ans=b[cnt-1],p=a[cnt-1]; 59 while(p--) { 60 pos=query(1,1,n1,k),n--; 61 if(!n) break; 62 if(q[pos].v>=0) k=(k-1+q[pos].v-1)%n+1;//下一个要ti的人当前的编号,很妙~ 63 else k=((k-1+q[pos].v)%n+n)%n+1;//mod +mod mod,转圈圈的题算完答案先mod一发再说~ 64 }//第一个k-1意思是从当前第一个开始数,第二个-1意思是当前的数已经被删除,如果要往右数应从上一个数开始 65 printf("%s %d\n", q[pos].s,ans); 66 } 67 return 0; 68 }