1. 程式人生 > 實用技巧 >Who Gets the Most Candies? POJ - 2886 (線段樹+篩法+模擬)

Who Gets the Most Candies? POJ - 2886 (線段樹+篩法+模擬)

Who Gets the Most Candies?

Nchildren are sitting in a circle to play a game.

The children are numbered from 1 toNin clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from theK-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. LetA

denote the integer. IfAis positive, the next child will be theA-th child to the left. IfAis 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, thep-th child jumping out will getF(p) candies whereF(p

) is the number of positive integers that perfectly dividep. Who gets the most candies?

Input

There are several test cases in the input. Each test case starts with two integersN(0 <N≤ 500,000) andK(1 ≤KN) on the first line. The nextNlines 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


題意:有n個小朋友順時針圍著坐了一圈,每個人手裡都有一個數字。
遊戲從第k個小朋友開始,首先,第k個小朋友退出遊戲,
如果退出遊戲的小朋友手上的數字是正整數A,則從他的左手邊的第A個小朋友退出遊戲,
如果退出遊戲的小朋友手上的數字是負整數-A,則從他的右手邊的第A個小朋友退出遊戲(注意,因為順時針,左手邊就是往後,右手邊就是往前)。
第i個退出的小朋友可以獲得a(i的約數的個數)的糖果,問最多糖果的小朋友的名字,並且他有多少糖果。


程式碼:
 1 #include <set>
 2 #include <map>
 3 #include <list>
 4 #include <stack>
 5 #include <queue>
 6 #include <deque>
 7 #include <cmath>
 8 #include <string>
 9 #include <vector>
10 #include <cstdio>
11 #include <cstring>
12 #include <cstdlib>
13 #include <sstream>
14 #include <iostream>
15 #include <algorithm>
16 //#include <unordered_map>
17 #define INF 0x3f3f3f3f
18 #define ll long long
19 #define ull unsigned long long
20 #define FILL(a,n,v) fill(a,a+n,v)
21 #define Mset(a,v) memset(a,v,sizeof a)
22 #define fcio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
23 
24 using namespace std;
25 const int maxn =5e5+10;
26 int F[maxn];//存約數個數
27 int sum[maxn<<2];
28 struct s
29 {
30     string name;
31     int num;
32 }child[maxn];
33 
34 void init()
35 {
36     FILL(F,maxn,2);
37     F[1]=1;
38     for(int i=2;i+i<maxn;i++)
39     {
40         for(int j=i+i;j<maxn;j+=i) F[j]++;
41     }
42 }
43 
44 void build(int l,int r,int root)
45 {
46     if(l==r)
47     {
48         sum[root]=1;
49         return ;
50     }
51     int mid=(r+l)>>1;
52     build(l,mid,root<<1);
53     build(mid+1,r,root<<1|1);
54     sum[root]=sum[root<<1]+sum[root<<1|1];//push_up
55 }
56 
57 int update(int pos,int l,int r,int root)
58 {
59     if(l==r)
60     {
61         sum[root]=0;
62         return l;
63     }
64     int mid=(r+l)>>1;
65     int res;
66     if(pos<=sum[root<<1]) res=update(pos,l,mid,root<<1);
67     else res=update(pos-sum[root<<1],mid+1,r,root<<1|1);
68     sum[root]=sum[root<<1]+sum[root<<1|1];//push_up
69     return res;
70 }
71 
72 int main()
73 {
74     fcio;
75     int n,k;
76     init();
77     while(cin>>n>>k)
78     {
79         build(1,n,1);
80         for(int i=1;i<=n;i++) cin>>child[i].name>>child[i].num;
81         int now=0;
82         int maxvalue=0;
83         string maxname;
84         for(int i=1;i<=n;i++)
85         {
86             now=update(k,1,n,1);
87             if(F[i]>maxvalue)
88             {
89                 maxvalue=F[i];
90                 maxname=child[now].name;
91             }
92             if(i==n) break;
93             if(child[now].num>0) k=(k+child[now].num-2)%sum[1]+1;
94             else k=((k+child[now].num-1)%sum[1]+sum[1])%sum[1]+1;
95         }
96         cout<<maxname<<' '<<maxvalue<<endl;
97     }
98     return 0;
99 }