# poj---（2886）Who Gets the Most Candies?（线段树+数论）

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

POJ Monthly--2006.07.30, Sempr

f(p)表示p的约数的个数....

不过要先将这些数据处理出来,在打表...不打表会超时.......

``` 1 //#define LOCAL
2 #include<cstdio>
3 #include<cstring>
4 #include<cstdlib>
5 using namespace std;
6 const int maxn=500005;
7 char str[maxn][20];
8 int sav[maxn];
9 //反素数
10 const int _prime[] = {1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,10080,15120,20160,25200,27720,45360,50400,55440,83160,110880,166320,221760,277200,332640,498960,554400};
11 //反素数个数
12 const int fac[] = {1,2,3,4,6,8,9,10,12,16,18,20,24,30,32,36,40,48,60,64,72,80,84,90,96,100,108,120,128,144,160,168,180,192,200,216};
13
14 struct node
15 {
16   int lef,rig;
17   int cnt;
18   int mid(){ return lef+((rig-lef)>>1); }
19 }seg[maxn<<2];
20
21 void build_seg(int left,int right ,int pos)
22 {
23      seg[pos].lef=left;
24      seg[pos].rig=right;
25      seg[pos].cnt=seg[pos].rig-seg[pos].lef+1;
26       if(left==right) return ;
27      int mid=seg[pos].mid();
28     build_seg(left,mid,pos<<1);
29     build_seg(mid+1,right,pos<<1|1);
30 }
31
32 int update(int pos,int num)
33 {
34     seg[pos].cnt--;
35     if(seg[pos].lef==seg[pos].rig)
36         return seg[pos].lef;
37     if(seg[pos<<1].cnt>=num)
38         return update(pos<<1,num);
39     else
40         return update(pos<<1|1,num-seg[pos<<1].cnt);
41 }
42
43 int main()
44 {
45   #ifdef LOCAL
46     freopen("test.in","r",stdin);
47   #endif
48   int n,k,i,cnt,pos;
49   while(scanf("%d%d",&n,&k)!=EOF)
50   {
51       for(i=1;i<=n;i++)
52     {
53       scanf("%s%d",str[i],&sav[i]);
54     }
55     build_seg(1,n,1);
56      cnt=0;
57     while(_prime[cnt]<n) cnt++;
58       if(_prime[cnt]>n)  cnt--;
59       sav[pos=0]=0;
60     for(i=0;i<_prime[cnt];i++)
61     {
62
63       if(sav[pos]>0)
64           k=((k+sav[pos]-2)%seg[1].cnt+seg[1].cnt)%seg[1].cnt+1;
65       else
66         k=((k+sav[pos]-1)%seg[1].cnt+seg[1].cnt)%seg[1].cnt+1;
67         pos=update(1,k);
68
69     }
70     printf("%s %d\n",str[pos],fac[cnt]);
71   }
72   return 0;
73 }```

0 条评论

## 相关文章

### HDU 1103 Flo's Restaurant(模拟+优先队列)

Flo's Restaurant Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/...

29860

### hdu----(5055)Bob and math problem(贪心)

Bob and math problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327...

30350

### POJ 2773 Happy 2006（容斥原理+二分）

Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 1...

31270

### HDUOJ----1003 Max Sum

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

26060

### HDU 3903 Trigonometric Function（数学定理）

Trigonometric Function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 1...

35080

### POJ-2039 To and Fro

To and Fro Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8...

22540

### HDUOJ---(4708)Rotation Lock Puzzle

Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327...

25340

### HDUOJ--1159Common Subsequence

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536...

27570

### HDUOJ------Daydream字符查找-并求其始末位置

2013-07-17  10:50:38 Daydream Time Limit: 2000/1000 MS (Java/Others)    Memory L...

29460

### HDUOJ----2647Reward

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja...

37980