poj---(2886)Who Gets the Most Candies?(线段树+数论)

时间:2023-11-28 14:29:32
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

There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 500,000) and K (1 ≤ KN) on the first line. The next N lines contains the names of the children (consisting of at most 10 letters) and the integers (non-zero with magnitudes within 108) on their cards in increasing order of the children’s numbers, a name and an integer separated by a single space in a line with no leading or trailing spaces.

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

提议: 有一群傻孩子以顺时针围坐成一个圈,开始的时候,第k个人站出来(圈外),并轮到该孩子的手中的第A个人,但是人坐着的时候为右边和左边
当A》0时,为左边起第A个人,否者为右边器第A个人.于是这样依次原来围城的圈人数越来越少(有点像约舍夫环),同事第p个人离开将会得到f(p)个糖果。
f(p)表示p的约数的个数....
  不过要先将这些数据处理出来,在打表...不打表会超时.......
代码:
 //#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 ;
}