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 2
Tom 2
Jack 4
Mary -1
Sam 1
Sample Output
Sam 3
Source
//#define LOCAL
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
char str[maxn][];
int sav[maxn];
//反素数
const int _prime[] = {,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
//反素数个数
const int fac[] = {,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}; struct node
{
int lef,rig;
int cnt;
int mid(){ return lef+((rig-lef)>>); }
}seg[maxn<<]; void build_seg(int left,int right ,int pos)
{
seg[pos].lef=left;
seg[pos].rig=right;
seg[pos].cnt=seg[pos].rig-seg[pos].lef+;
if(left==right) return ;
int mid=seg[pos].mid();
build_seg(left,mid,pos<<);
build_seg(mid+,right,pos<<|);
} int update(int pos,int num)
{
seg[pos].cnt--;
if(seg[pos].lef==seg[pos].rig)
return seg[pos].lef;
if(seg[pos<<].cnt>=num)
return update(pos<<,num);
else
return update(pos<<|,num-seg[pos<<].cnt);
} int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int n,k,i,cnt,pos;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%s%d",str[i],&sav[i]);
}
build_seg(,n,);
cnt=;
while(_prime[cnt]<n) cnt++;
if(_prime[cnt]>n) cnt--;
sav[pos=]=;
for(i=;i<_prime[cnt];i++)
{ if(sav[pos]>)
k=((k+sav[pos]-)%seg[].cnt+seg[].cnt)%seg[].cnt+;
else
k=((k+sav[pos]-)%seg[].cnt+seg[].cnt)%seg[].cnt+;
pos=update(,k); }
printf("%s %d\n",str[pos],fac[cnt]);
}
return ;
}