Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 14121 | Accepted: 4461 | |
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
题意:类似约瑟夫环那样,每个人有个值,是如果他跳出来之后,从他这开始算下一个该跳出来的人的序号。只不过每个人跳出来的时候会得到一些糖果,而得到的糖果的数目等于f(p),是指第p次跳出的人能够获得p的因数的个数的糖果。
思路:先找出第几次跳出能够获得最大的糖果数id。
然后模拟n次,第n次跳出的人的名字输出,然后输出最大的糖果数就行了。
终点就是用线段树维护这个约瑟夫环。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=5e5+7;
int n,k,id;
struct node
{
int l,r;
int sum;//当前区间还有多少个人
}tree[MAXN<<2];
struct node1
{
char name[20];
int val;
}people[MAXN];
int ans[MAXN];
void get_ans()//每个数的因数的个数
{
int i,j;
for(i=1;i<=500000;++i)
{
ans[i]++;
for(j=i*2;j<=500000;j+=i)ans[j]++;
}
}
void get_id()
{
int MAX=1;
for(int i=2;i<=n;++i)
{
if(ans[i]>MAX)
{
MAX=ans[i];
id=i;
}
}
}
void build_tree(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
tree[i].sum=r-l+1;
if(l==r)return;
int mid=(l+r)>>1;
build_tree(i<<1,l,mid);
build_tree(i<<1|1,mid+1,r);
}
int del(int i,int key)//线段树维护删除人
{
tree[i].sum--;
if(tree[i].l==tree[i].r)return tree[i].l;
if(tree[i<<1].sum>=key)return del(i<<1,key);
else return del(i<<1|1,key-tree[i<<1].sum);
}
int main()
{
int i;
get_ans();
while(~scanf("%d%d",&n,&k))
{
get_id();
for(i=1;i<=n;++i)
{
scanf("%s %d",people[i].name,&people[i].val);
}
build_tree(1,1,n);
int mod=tree[1].sum;
n=id;
int pos=0;
people[0].val=0;
while(n--)
{
if(people[pos].val>0)
{
k=((k-1+people[pos].val-1)%mod+mod)%mod+1;
}
else k=((k-1+people[pos].val)%mod+mod)%mod+1;
pos=del(1,k);
mod--;
}
printf("%s %d\n",people[pos].name,ans[id]);
}
return 0;
}