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

Who Gets the Most Candies?(线段树 + 反素数 )

时间:2015-02-06 21:39:57      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

Who Gets the Most Candies?
Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u
Submit Status

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 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. 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

 

 先用反素数求出1~n中拥有最多因子的数x,从而得到:在第x人出队时 ,最多的candy数maxn 
技术分享
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 #define lson o << 1 , l , mid
 5 #define rson o << 1 | 1 , mid + 1 , r
 6 using namespace std;
 7 //list anti prime
 8 int s[40] = {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,500001};
 9 int b[40] = {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,1314521};
10 int k , n ;
11 char name[500111][11] ;
12 int val[500111] ;
13 int a[2000100];
14 
15 void init (int o , int l , int r )
16 {
17     a[o] = r - l + 1 ;
18     if ( l == r ) {
19         return ;
20     }
21     int mid = ( l + r ) >> 1 ;
22     init ( lson ) ;
23     init ( rson ) ;
24 }
25 
26 int insert (int x , int o , int l , int r )
27 {
28     a[o]-- ;
29     if ( l == r ) {
30         return l ;
31     }
32     int mid = ( l + r ) >> 1 ;
33     if ( x <= a[o << 1] ) {
34         insert (x , o << 1 , l , mid ) ;
35     }
36     else {
37         insert ( x - a[o << 1] , o << 1 | 1 , mid + 1 , r ) ;
38     }
39 }
40 
41 int main ()
42 {
43   //  freopen ( "a.txt" , "r" , stdin ) ;
44     int i , j , maxn , p , pos ;
45     while (~ scanf ("%d%d" , &n , &k ) ) {
46         i = 0 ;
47         while ( s[i] <= n ) {
48             i++ ;
49         }
50         maxn = b[i - 1] ;
51         p = s[i - 1] ;//p-th leave the team must be the most , and was getting as much Max candy the first team , then
52         //we obtain its number of factor
53         for ( i = 1 ; i <= n ; i++ ) {
54             scanf ( "%s%d" , name[i] , &val[i] ) ;
55         }
56         init (1 , 1 , n ) ;
57         i = n ;
58         while ( p-- ) {//so we can only query p childs
59             n-- ;
60             pos = insert ( k , 1 , 1 , i) ;
61             if ( !n ) {
62                 break ;
63             }
64             if ( val[pos] >= 0 ) {//clockwise
65                 k = ( k - 1 + val[pos] - 1 ) % n + 1 ;//relates to the modular arithmetic , make the code from the begining of ‘0‘ ,
66                 //so start to minus one , as with list Vacancy Segment tree , the obtained ‘k‘ is the new tree i-th location
67             }
68             else {//Anti-clockwise
69                 k = ( ( k - 1 + val[pos] ) % n + n ) % n + 1 ;
70             }
71         }
72         printf ( "%s %d\n" , name[pos] , maxn ) ;
73     }
74     return 0 ;
75 }
反素数的应用

 

Who Gets the Most Candies?(线段树 + 反素数 )

标签:

原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4277979.html

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