首页 > 代码库 > (线段树,反素数)poj2886-Who Gets the Most Candies?

(线段树,反素数)poj2886-Who Gets the Most Candies?

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 ≤ K ≤ N) 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

线段树的每个结点表示代表的区间中有多少个人还在。分别记每次该出去的人的相对位置(即剩下的x个人1——x编号,第z号出去),以及这个人的绝对位置。
 1 #include <iostream>
 2 //#include<bits/stdc++.h>
 3 #include <stack>
 4 #include <queue>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <algorithm>
 8 using namespace std;
 9 typedef long long ll;
10 typedef unsigned long long ull;
11 const int MAX=5e5+5;
12 const int antiprime[]={1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,
13                        1260,1680,2520,5040,7560,10080,15120,20160,25200,
14                        27720,45360,50400,55440,83160,110880,166320,221760,
15                        277200,332640,498960,554400,665280
16                       };
17 
18 const int factorNum[]={1,2,3,4,6,8,9,10,12,16,18,20,24,30,32,36,40,48,60,
19                        64,72,80,84,90,96,100,108,120,128,144,160,168,180,
20                        192,200,216,224
21                       };
22 struct node
23 {
24     char name[10];
25     int num;
26 }ren[MAX];
27 int st[10*MAX];
28 void init(int l,int r,int k)
29 {
30     st[k]=r-l;
31     if(l+1==r)
32         return;
33     init(l,(l+r)/2,2*k);
34     init((l+r)/2,r,2*k+1);
35 }
36 void push(int k)
37 {
38     if(k<=0)
39         return;
40     st[k]--;
41     push(k/2);
42 }
43 int update(int dis,int l,int r,int k)
44 {
45     if(l+1==r)
46     {
47         push(k);
48         return l;
49     }
50     if(dis<=st[2*k])
51         return update(dis,l,(l+r)/2,2*k);
52     else
53         return update(dis-st[2*k],(l+r)/2,r,2*k+1);
54 }
55 int main()
56 {
57     int n,k;//n为初始人数,k为每次该出去的人的相对序号(即此时剩的人编上1——x时的序号)
58     int cnt;
59     int i;
60     int pos;//该出去的人的真实序号(初始时这个人的编号)
61     while(~scanf("%d %d",&n,&k))
62     {
63         init(1,n+1,1);
64         for(i=1;i<=n;i++)
65         {
66             scanf("%s %d",ren[i].name,&ren[i].num);
67         }
68         cnt=0;
69         while(n>=antiprime[cnt])
70             cnt++;
71         cnt--;
72         pos=0;
73         ren[0].num=0;
74         for(i=1;i<=antiprime[cnt];i++)
75         {
76             if(ren[pos].num>0)
77                 k=((k+ren[pos].num-2)%st[1]+st[1])%st[1]+1;//此时st[1]个人,k值为1——st[1],不能为0,故进行此操作
78             else
79                 k=((k+ren[pos].num-1)%st[1]+st[1])%st[1]+1;
80             pos=update(k,1,n+1,1);
81         }
82         printf("%s %d\n",ren[pos].name,factorNum[cnt]);
83     }
84 }

 

(线段树,反素数)poj2886-Who Gets the Most Candies?