题意:N个小孩围成一圈,他们被顺时针编号为 1 到 N。每个小孩手中有一个卡片,上面有一个非 0 的数字,游戏从第 K 个小孩开始,他告诉其他小孩他卡片上的数字并离开这个圈,他卡片上的数字 A 表明了下一个离开的小孩,如果 A 是大于 0 的,则下个离开的是左手边第 A 个,如果是小于 0 的,则是右手边的第 A 个小孩。游戏将直到所有小孩都离开,在游戏中,第 p 个离开的小孩将得到 F(p) 个糖果,F(p) 是 p 的约数的个数,问谁将得到最多的糖果。输出最幸运的小孩的名字和他可以得到的糖果。
解法:线段树维护当前区间的小孩个数。关键在于下一步哪个孩子出去的公式。
用一个变量记录当前要出去的小孩的相对位置,每次根据相对位置找出其编号,这样求解简便。
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 14646 | Accepted: 4617 | |
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
代码:
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define ysk(x) (1<<(x))
typedef long long ll;
const int N=500000;
int n,fir;
int numF[N+5],best[N+5];
char name[N+5][14];int step[N+5];
void init()
{
int limit=sqrt(N+0.5);
for(int i=1;i<=limit;i++)
{
numF[i*i]++;
for(int j=i+1;j*i<=N;j++)
{
numF[i*j]+=2;
}
}
int ans=-1,maxi=-1;
for(int i=1;i<=N;i++)
{
if(numF[i]>maxi) { maxi=numF[i];ans=i; }
best[i]=ans;
}
}
struct SegmentTree
{
int L[4*N],R[4*N],C[4*N];
void build(int ind,int le,int ri)
{
L[ind]=le;R[ind]=ri;
if(le==ri)
{
C[ind]=1;
return;
}
int mid=(le+ri)>>1;
build(ind<<1,le,mid);
build(ind<<1|1,mid+1,ri);
C[ind]= C[ind<<1]+C[ind<<1|1];
}
int find(int ind,int num)
{
int le=L[ind],ri=R[ind];
if(le==ri)
{
C[ind]=0;
return le;
}
int mid=(le+ri)>>1;
int ans;
if(num<=C[ind<<1]) ans= find(ind<<1,num);
else ans= find(ind<<1|1,num-C[ind<<1]);
C[ind]=C[ind<<1]+C[ind<<1|1];
return ans;
}
}sgt;
int main()
{
init();
while(~scanf("%d%d",&n,&fir))
{
for1(i,n) scanf("%s%d",name[i],&step[i]);
sgt.build(1,1,n);
int now=fir,pos;
for1(i,best[n])
{
pos=sgt.find(1,now);int mov=step[pos];
int cur_num=n-i;if(!cur_num) break;
if(mov>0) now=( ( now-1 + mov -1 )%cur_num+cur_num )%cur_num+1;
else now=( ( now + mov -1 )%cur_num+cur_num )%cur_num+1;
}
printf("%s %d\n",name[pos],numF[best[n]]);
}
return 0;
}