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的约数的个数....
  不过要先将这些数据处理出来,在打表...不打表会超时.......
代码:
 //#define LOCAL
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
char str[maxn][];
int sav[maxn];
//反素数
const int _prime[] = {,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
//反素数个数
const int fac[] = {,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}; struct node
{
int lef,rig;
int cnt;
int mid(){ return lef+((rig-lef)>>); }
}seg[maxn<<]; void build_seg(int left,int right ,int pos)
{
seg[pos].lef=left;
seg[pos].rig=right;
seg[pos].cnt=seg[pos].rig-seg[pos].lef+;
if(left==right) return ;
int mid=seg[pos].mid();
build_seg(left,mid,pos<<);
build_seg(mid+,right,pos<<|);
} int update(int pos,int num)
{
seg[pos].cnt--;
if(seg[pos].lef==seg[pos].rig)
return seg[pos].lef;
if(seg[pos<<].cnt>=num)
return update(pos<<,num);
else
return update(pos<<|,num-seg[pos<<].cnt);
} int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int n,k,i,cnt,pos;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%s%d",str[i],&sav[i]);
}
build_seg(,n,);
cnt=;
while(_prime[cnt]<n) cnt++;
if(_prime[cnt]>n) cnt--;
sav[pos=]=;
for(i=;i<_prime[cnt];i++)
{ if(sav[pos]>)
k=((k+sav[pos]-)%seg[].cnt+seg[].cnt)%seg[].cnt+;
else
k=((k+sav[pos]-)%seg[].cnt+seg[].cnt)%seg[].cnt+;
pos=update(,k); }
printf("%s %d\n",str[pos],fac[cnt]);
}
return ;
}

poj---(2886)Who Gets the Most Candies?(线段树+数论)的更多相关文章

  1. POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)

    线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...

  2. POJ 2886 Who Gets the Most Candies?(线段树&#183;约瑟夫环)

    题意  n个人顺时针围成一圈玩约瑟夫游戏  每一个人手上有一个数val[i]   開始第k个人出队  若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人   val[k ...

  3. POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感

    这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感.... 和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错.... 题意:从第K个同学开始,若K的数字为正 则往右转, ...

  4. POJ 2886 Who Gets the Most Candies? 线段树

    题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ...

  5. poj 2886 "Who Gets The Most Candies?"(树状数组)

    传送门 参考资料: [1]:http://www.hankcs.com/program/algorithm/poj-2886-who-gets-the-most-candies.html 题意: 抢糖 ...

  6. 线段树(单点更新) POJ 2886 Who Gets the Most Candies?

    题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...

  7. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  8. POJ 2886 Who Gets the Most Candies? (线段树)

    [题目链接] http://poj.org/problem?id=2886 [题目大意] 一些人站成一个圈,每个人手上都有一个数字, 指定从一个人开始淘汰,每次一个人淘汰时,将手心里写着的数字x展示 ...

  9. (中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

  10. POJ 2886 Who Gets the Most Candies?(反素数+线段树)

    点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...

随机推荐

  1. MySQL复制表结构表数据

    MySQL复制表结构 表数据 1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete ...

  2. tophat cufflinks cuffcompare cuffmerge 的使用

    Cole Trapnell said: there are three strategies: 1) merge bams and assemble in a single run of Cuffli ...

  3. 字段符号FIELD-SYMBOLS

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. Python基础学习笔记(一)入门

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  5. input与select 设置相同宽高,在浏览器上却显示不一致,不整齐

    遇到 input与select 设置相同宽高,在浏览器上却显示不一致,遂实验了下(IE 10.013 ,Firefox 30.0),得出以下结论 input   width,height 值里面, 不 ...

  6. 2013 Multi-University Training Contest 10

    HDU-4698 Counting 题意:给定一个二维平面,其中x取值为1-N,y取值为1-M,现给定K个点,问至少包括K个点中的一个的满足要求的<Xmin, Xmax, Ymin, Ymax& ...

  7. html一般标签、常用标签、表格

    body的属性: bgcolor               页面背景色 text                    文字颜色 topmargin            上边距 leftmargi ...

  8. 常用sql(转)

    1增 1.1[插入单行]insert [into] <表名> (列名) values (列值)例:insert into Strdents (姓名,性别,出生日期) values ('开心 ...

  9. shiro-web整合

    1.所需要jar <!-- shiro核心包 --> <dependency> <groupId>org.apache.shiro</groupId> ...

  10. [转载] Linux下高并发socket最大连接数所受的各种限制

    原文: http://mp.weixin.qq.com/s?__biz=MzAwNjMxNjQzNA==&mid=207772333&idx=1&sn=cfc8aadb422f ...