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个离 ...
随机推荐
- Hadoop分布式部署——要点
这里只记录几个要点,比较容易出问题的地方. 1.各服务器必须有相同的用户(便于使用相同的用户ssh登录)2.ssh互通,配置无密码登录ssh-keygen -t rsa,将id_rsa.pub的内容相 ...
- Python中时间的处理之——timedelta篇
#! /usr/bin/python # coding=utf-8 from datetime import datetime,timedelta """ timed ...
- [HDOJ5726]GCD(RMQ,二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:给定数列,求区间[L,R]的GCD的值,并求出有多少个子区间满足和[L,R]的GCD相等. ...
- C# 线程(三):如何操纵一个线程
From : http://kb.cnblogs.com/page/42529/ 下面我们就动手来创建一个线程,使用Thread类创建线程时,只需提供线程入口即可.(线程入口使程序知道该让这个线程干什 ...
- iOS - OC SQLite 数据库存储
前言 采用 SQLite 数据库来存储数据.SQLite 作为一中小型数据库,应用 iOS 中,跟前三种保存方式相比,相对比较复杂一些. 注意:写入数据库,字符串可以采用 char 方式,而从数据库中 ...
- mysql概要(九)字符集和校对集
1.mysql 字符集有细致设置: 2.mysql字符处理机制是:数据库和客户端之间存在一个字符集转换器(后文简称转换器)将客户端字符编码(必须告诉服务端的)转换成一种中间编码的数据(可自定义的但保证 ...
- c function
/* #include<stdio.h> int is_prime(int n) { for(int i = 2; i <= n/2; i ++) if(n % 2 == 0) re ...
- [转载] 对象存储(2):OpenStack Swift——概念、架构与规模部署
原文: http://www.testlab.com.cn/Index/article/id/1085.html#rd?sukey=fc78a68049a14bb228cb2742bdec2b9498 ...
- Codeforces708C Centroids 【树形dp】
题目链接 题意:给定一棵n个结点的树,问:对于每个结点,能否通过删除一条边并添加一条边使得仍是树,并且删除该结点后得到的各个连通分量结点数 <= n/2? 题解:树形dp,两遍dfs,第一遍df ...
- Oracle 逐条和批量插入数据方式对比
创建测试表 create table base_users ( userid varchar2(16), username varchar2(32), passwd var ...