码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-10-12 01:18:06      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

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的约数的个数....
  不过要先将这些数据处理出来,在打表...不打表会超时.......
代码:
bubuko.com,布布扣
 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 }
View Code

 

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

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/gongxijun/p/4019878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!