poj---(2886)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
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
//#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?(线段树+数论)的更多相关文章
- POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)
线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...
- POJ 2886 Who Gets the Most Candies?(线段树·约瑟夫环)
题意 n个人顺时针围成一圈玩约瑟夫游戏 每一个人手上有一个数val[i] 開始第k个人出队 若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人 val[k ...
- POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感
这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感.... 和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错.... 题意:从第K个同学开始,若K的数字为正 则往右转, ...
- POJ 2886 Who Gets the Most Candies? 线段树
题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ...
- poj 2886 "Who Gets The Most Candies?"(树状数组)
传送门 参考资料: [1]:http://www.hankcs.com/program/algorithm/poj-2886-who-gets-the-most-candies.html 题意: 抢糖 ...
- 线段树(单点更新) POJ 2886 Who Gets the Most Candies?
题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
- POJ 2886 Who Gets the Most Candies? (线段树)
[题目链接] http://poj.org/problem?id=2886 [题目大意] 一些人站成一个圈,每个人手上都有一个数字, 指定从一个人开始淘汰,每次一个人淘汰时,将手心里写着的数字x展示 ...
- (中等) 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 ...
- POJ 2886 Who Gets the Most Candies?(反素数+线段树)
点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...
随机推荐
- Cheatsheet: 2014 01.01 ~ 01.14
.NET 15 reasons why I can't work without JetBrains ReSharper Web Web scraping with Node.js Koa.js : ...
- C#在泛型类中,通过表达式树构造lambda表达式
场景 最近对爬虫的数据库架构做调整,需要将数据迁移到MongoDB上去,需要重新实现一个针对MongoDB的Dao泛型类,好吧,动手开工,当实现删除操作的时候问题来了. 我们的删除操作定义如下:voi ...
- linux tar命令
tar命令打包还是压缩需要看所调用的命令参数....tar在使用时可以调用命令参数, 比如tar -xvf +文件名就是解包,但是不是解压...只有在使用了参数z等调用gzip等 压缩命令时才是压缩或 ...
- python_way ,day2 字符串,列表,字典,时间模块
python_way ,day2 字符串,列表,字典,自学时间模块 1.input: 2.0 3.0 区别 2.0中 如果要要用户交互输入字符串: name=raw_input() 如果 name=i ...
- STORM_0010_Message passing implementation/消息传递的实现
下面是0.8.0之前的表述,之后的已经基于Disruptor改造过了 这个文章演示了发射和转移tuple是怎么在storm中工作的 Worker为消息传递负责 当zk中的任务出现了变化或者每个ta ...
- Tomcat配置虚拟目录
在Tomcat7版本下,配置虚拟路径修改以下两个文件: 1.server.xml 打开Tomcat目录下的/conf/server.xml文件,在Host之前加入下面红色部分的内容. ...
- Xstream 学习地址
http://forestqqqq.iteye.com/category/301129
- shell script创建库
先创建名称为 myfuns # my script functions function addem { + $ ] } function multem { * $ ] } function dive ...
- Jquery基本、层次选择器
基本选择器: $("#none").css("background","#bbffaa"); 改变id为none的所有元素的背景色 $(&q ...
- 《易货》Alpha版本发布说明
一.引言 本说明描述了校淘1.0版本的已经实现的主要功能,对运行环境的要求,以及目前软件所具有的一些问题和限制. 二.主要功能 本软件面向的是在校大学生,方便大学生将自己闲置的物品以二手商品的形式发布 ...