传送门:点击打开链接
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 10852 | Accepted: 3371 | |
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
题意:有n个熊孩子站成一圈玩游戏,顺时针编号从1~n。他们每个人有一个数字(不为0),当第i个人出圈的时候,如果他的数为正数x,则下一个出圈的是从这个人开始(这个人不算)顺时针数的第xi个人。为负数则是逆时针。第i个出圈的熊孩子获得i的因子的个数个糖果。问获得糖果最多的最先出圈的是谁。
思路:用线段树记录区间内剩余的人数。譬如查找第x个人,如果左区间的人数小于等于x,就递归到左区间,否则就递归到右区间寻找第x-(左区间人数)个人。返回其最初的编号。用k记录实际是第几个人出圈,当这个人手里为正数的时候,他出圈了,那么后面人的时间编号都会减1,这里要十分地注意!
代码:
#include<cstdio>
#include<cstring>
int sum[2000005];
char name[500005][12];
int num[500005];
int tmp[500005];
int ans;
void init(int n=500000)
{
memset(tmp,0,sizeof(tmp));
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j+=i) tmp[j]++;
}
void init2(int n)
{
ans=0;
for(int i=1;i<=n;i++) if(ans<tmp[i]) ans=tmp[i];
}
void build(int id,int L,int R)
{
sum[id]=R-L+1;
if(L==R) return;
else
{
int mid=(L+R)>>1;
build(id<<1,L,mid);
build(id<<1|1,mid+1,R);
}
}
int que(int id,int L,int R,int c)
{
if(L==R)
{
sum[id]--;
return L;
}
else
{
sum[id]--;
int mid=(L+R)>>1;
if(c<=sum[id<<1]) return que(id<<1,L,mid,c);
else return que(id<<1|1,mid+1,R,c-sum[id<<1]);
}
}
int main()
{
init();
int n,k;
while(~scanf("%d %d",&n,&k))
{
init2(n);
for(int i=1;i<=n;i++)
{
scanf("%s %d",name[i],&num[i]);
}
build(1,1,n);
int pos=k;
for(int i=1;i<=n;i++)
{
pos=que(1,1,n,k);
if(tmp[i]==ans)
{
printf("%s %d\n",name[pos],ans);
break;
}
if(num[pos]>0)
{
k=(k-1+num[pos]-1)%(n-i)+1;
}
else
{
k=((k-1+num[pos])%(n-i)+(n-i))%(n-i)+1;
}
}
}
return 0;
}