POJ 2886 Who Gets the Most Candies?
Who Gets the Most Candies?
64-bit integer IO format: %lld Java class name: Main
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 500010; 6 int tree[maxn<<2],val[maxn],ans[maxn],n,k; 7 char name[maxn][16]; 8 void pushup(int v) { 9 tree[v] = tree[v<<1] + tree[v<<1|1]; 10 } 11 void build(int L,int R,int v) { 12 if(L == R) { 13 tree[v] = 1; 14 return; 15 } 16 int mid = (L + R)>>1; 17 build(L,mid,v<<1); 18 build(mid+1,R,v<<1|1); 19 pushup(v); 20 } 21 int query(int L,int R,int lt,int rt,int v) { 22 if(lt <= L && rt >= R) return tree[v]; 23 int mid = (L + R)>>1,ans = 0; 24 if(lt <= mid) ans += query(L,mid,lt,rt,v<<1); 25 if(rt > mid) ans += query(mid,R,lt,rt,v<<1|1); 26 return ans; 27 } 28 int update(int L,int R,int p,int v) { 29 if(L == R) { 30 tree[v]--; 31 return L; 32 } 33 int mid = (L + R)>>1,o; 34 if(tree[v<<1] >= p) o = update(L,mid,p,v<<1); 35 else o = update(mid+1,R,p-tree[v<<1],v<<1|1); 36 pushup(v); 37 return o; 38 } 39 void preSolve() { 40 for(int i = 1; i < maxn; ++i) { 41 ans[i]++; 42 for(int j = i<<1; j < maxn; j += i) 43 ans[j]++; 44 } 45 } 46 int main() { 47 preSolve(); 48 while(~scanf("%d %d",&n,&k)) { 49 for(int i = 1; i <= n; ++i) 50 scanf("%s %d",name[i],val+i); 51 build(1,n,1); 52 int ret = 0,id = 0,pos = k; 53 for(int i = 1; i <= n; ++i) { 54 int o = update(1,n,k,1); 55 if(ans[i] > ret) { 56 ret = ans[i]; 57 id = o; 58 } 59 if(i == n) break; 60 if(val[o] > 0) 61 k = ((val[o] + k - 1)%tree[1] + tree[1])%tree[1]; 62 else if(val[o] < 0) 63 k = ((k + val[o])%tree[1] + tree[1])%tree[1]; 64 if(k == 0) k = tree[1]; 65 } 66 printf("%s %d\n",name[id],ret); 67 } 68 return 0; 69 }