Who Gets the Most Candies?
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 11303 | Accepted: 3520 | |
| 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
(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
这道题可以用线段树做,是很有趣的一道,用到了约瑟夫环和反素数表。反素数的定义:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数·在这道题中,要求的就是不超过n的最大反素数p,因为它的因数最多,即只要找出第p个跳出孩子的编号。另一个用到的是约瑟夫环公式,设跳出的小孩编号是k,当孩子拿的号码是正数A时,如果A=1,那么下个要跳出的编号是K,此时对应的A是1,那么可设起点编号是k-1,对应的A是0,则其通项公式是k=(k-1+A-1)%n+1;(这里采用先顺时针走A-1步,再顺时针走最后一步,是为了避免出现取余后结果是0的情况),如果A小于0,那么同理可以得到对应编号为0所对应的编号为k-1,采用先逆时针走-A-1,在顺时针走一步的方法,得到公式k=((k-1+A)%n+n)%n+1;注意这里的A对应的是最初原始排列的小孩手里拿的编号,是搜到的线段树的叶节点。
#include<stdio.h>
#include<string.h>
char s[500005][11];
int num[500005];
int a[37]={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};
int c[37]={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};
struct node
{
int l,r,pos;
}b[4*500005];
void build(int l,int r,int i)
{
int mid;
b[i].l=l;b[i].r=r;
if(l==r){
b[i].pos=1;return;
}
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
b[i].pos=b[i*2].pos+b[i*2+1].pos;
} int update(int pos,int i)
{
int mid;
if(b[i].l==b[i].r){
b[i].pos=0;return b[i].l;
}
b[i].pos--;
if(b[i*2].pos>=pos){
return update(pos,i*2);
}
else return update(pos-b[i*2].pos,i*2+1);
} int main()
{
int n,m,i,j,max,p,k,h;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(s,0,sizeof(s));
memset(num,0,sizeof(num));
build(1,n,1);
for(i=1;i<=n;i++){
scanf("%s%d",s[i],&num[i]);
}
i=1;
while(a[i]<=n){
i++;
}
max=c[i-1];
p=a[i-1];
for(i=1;i<=p;i++){
n--;
h=update(k,1); //h是最初始孩子的编号,也是线段树的叶节点
if(n==0)break; //很重要!
if(num[h]>0)
k=(k-1+num[h]-1)%n+1;
else k=((k-1+num[h])%n+n)%n+1;
//printf("%d\n",h);
}
printf("%s %d\n",s[h],max);
}
return 0;
}
Who Gets the Most Candies?的更多相关文章
- 【POJ2886】Who Gets the Most Candies?-线段树+反素数
		Time Limit: 5000MS Memory Limit: 131072K Case Time Limit: 2000MS Description N children are sitting ... 
- poj 3159 Candies 差分约束
		Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ... 
- Who Gets the Most Candies?(线段树 + 反素数 )
		Who Gets the Most Candies? Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d &am ... 
- poj---(2886)Who Gets the Most Candies?(线段树+数论)
		Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10373 Acc ... 
- poj3159 Candies(差分约束,dij+heap)
		poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ... 
- HDU 5127 Dogs' Candies
		Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ... 
- C. Om Nom and Candies   巧妙优化枚举,将复杂度控制在10e6
		C. Om Nom and Candies 无线超大背包问题 #include <iostream> #include <cstdio> #include <cstrin ... 
- POJ 3159 Candies (栈优化spfa)
		Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ... 
- POJ 3159 Candies(差分约束,最短路)
		Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ... 
- (poj)3159 Candies
		题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ... 
随机推荐
- LRU缓存的实现
			文章目录 LRU简介 LRU算法分析 实现代码 节点类 双向链表 LRUCache类 测试类 总结 LRU简介 LRU是"Least Recently Used"的简写,意思是最近 ... 
- LeetCode202. 快乐数
			题目 编写一个算法来判断一个数 n 是不是快乐数. 快乐数定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1, 也可能是 无限循环 但始终变不到 ... 
- 攻防世界 - Crypto(一)
			base64: 根据题目base64可知编码方式,下载附件发现是一个txt文件,把内容用工具解码就彳亍了,即可得到flag, flag: cyberpeace{Welcome_to_new_World ... 
- Databricks 第7篇:管理Secret
			有时,访问数据要求您通过JDBC对外部数据源进行身份验证,可以使用Azure Databricks Secret来存储凭据,并在notebook和job中引用它们,而不是直接在notebook中输入凭 ... 
- 单元测试:单元测试中的mock
			公司要求提升单元测试的质量,提高代码的分支覆盖率和行覆盖率,安排我研究单元测试,指定方案分享并在开发部普及开.整理完资料后,同步一下到博客. 单元测试中的mock的目的 mock的主要目的是让单元测试 ... 
- Python批量 png转ico
			Python 批量 png 转 ico 一.前言: 首先说一下ico文件的作用:ico是windows的图标文件格式,可以用于浏览器首段图标显示,也可以用于Windows软件.我的话一般用来美化文件夹 ... 
- 鸿蒙的fetch请求加载聚合数据的前期准备工作-手动配置网络权限
			目录: 1.双击打开"config.json"文件 2.找到配置网络访问权限位置1 3.配置内容1 4.默认访问内容是空的 5.添加配置内容2 6.复制需要配置的网络二级URL 7 ... 
- kafka(一)入门
			一.消息引擎系统 这类系统引以为豪的消息传递属性,像引擎一样,具备某种能量转换传输的能力 消息引擎系统是一组规范,企业利用这组规范在不同系统之间传递语义准确的消息,实现松耦合的异步式数据传递.通俗地讲 ... 
- 入门OJ:郭嘉的消息传递
			题目描述 我们的郭嘉大大在曹操这过得逍遥自在,但是有一天曹操给了他一个任务,在建邺城内有N(<=1000)个袁绍的奸细 将他们从1到N进行编号,同时他们之间存在一种传递关系,即若C[i,j]=1 ... 
- .net core 和 WPF 开发升讯威在线客服与营销系统:使用 TCP协议 实现稳定的客服端
			本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ... 
