首页 > 代码库 > poj---(2886)Who Gets the Most Candies?(线段树+数论)
poj---(2886)Who Gets the Most Candies?(线段树+数论)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 10373 | Accepted: 3224 | |
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 2Tom 2Jack 4Mary -1Sam 1
Sample Output
Sam 3
Source
1 //#define LOCAL 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 const int maxn=500005; 7 char str[maxn][20]; 8 int sav[maxn]; 9 //反素数10 const int _prime[] = {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,554400};11 //反素数个数12 const int fac[] = {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,216};13 14 struct node15 {16 int lef,rig;17 int cnt;18 int mid(){ return lef+((rig-lef)>>1); }19 }seg[maxn<<2];20 21 void build_seg(int left,int right ,int pos)22 {23 seg[pos].lef=left;24 seg[pos].rig=right;25 seg[pos].cnt=seg[pos].rig-seg[pos].lef+1;26 if(left==right) return ;27 int mid=seg[pos].mid();28 build_seg(left,mid,pos<<1);29 build_seg(mid+1,right,pos<<1|1);30 }31 32 int update(int pos,int num)33 {34 seg[pos].cnt--;35 if(seg[pos].lef==seg[pos].rig)36 return seg[pos].lef;37 if(seg[pos<<1].cnt>=num)38 return update(pos<<1,num);39 else40 return update(pos<<1|1,num-seg[pos<<1].cnt);41 }42 43 int main()44 {45 #ifdef LOCAL46 freopen("test.in","r",stdin);47 #endif48 int n,k,i,cnt,pos;49 while(scanf("%d%d",&n,&k)!=EOF)50 {51 for(i=1;i<=n;i++)52 {53 scanf("%s%d",str[i],&sav[i]);54 }55 build_seg(1,n,1);56 cnt=0;57 while(_prime[cnt]<n) cnt++;58 if(_prime[cnt]>n) cnt--;59 sav[pos=0]=0;60 for(i=0;i<_prime[cnt];i++)61 {62 63 if(sav[pos]>0)64 k=((k+sav[pos]-2)%seg[1].cnt+seg[1].cnt)%seg[1].cnt+1;65 else66 k=((k+sav[pos]-1)%seg[1].cnt+seg[1].cnt)%seg[1].cnt+1;67 pos=update(1,k);68 69 }70 printf("%s %d\n",str[pos],fac[cnt]);71 }72 return 0;73 }
poj---(2886)Who Gets the Most Candies?(线段树+数论)