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 ≤ 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
Source
POJ Monthly--2006.07.30, Sempr
提议: 有一群傻孩子以顺时针围坐成一个圈,开始的时候,第k个人站出来(圈外),并轮到该孩子的手中的第A个人,但是人坐着的时候为右边和左边
当A》0时,为左边起第A个人,否者为右边器第A个人.于是这样依次原来围城的圈人数越来越少(有点像约舍夫环),同事第p个人离开将会得到f(p)个糖果。
f(p)表示p的约数的个数....
于是先看看这一篇文章...http://user.qzone.qq.com/1005267096/blog/1413018036
不过要先将这些数据处理出来,在打表...不打表会超时.......
代码:
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 %dn",str[pos],fac[cnt]);
71 }
72 return 0;
73 }
- 小错误系列
- HDUOJ-----4510 小Q系列故事——为什么时光不能倒流
- HDUOJ----4509湫湫系列故事——减肥记II
- HDUOJ-----4506小明系列故事——师兄帮帮忙
- HDUOJ---4503 湫湫系列故事——植树节
- HDUOJ----4504 威威猫系列故事——篮球梦
- 推荐一款MySQL优化工具
- HDUOJ----4502吉哥系列故事——临时工计划
- HDUOJ----4004The Frog's Games(二分+简单贪心)
- HDUOJ----4006The kth great number(最小堆...)
- HDUOJ----4501小明系列故事——买年货(三维背包)
- message 弹出窗口
- HDUOJ---1862EXCEL排序
- HDUOJ-----3591The trouble of Xiaoqian
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法