POJ 2886 Who Gets the Most Candies?(反素数+线段树)
题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离开的孩子得到的糖果是f(p)个,f(p)是p的约数的个数。
思路 : 因为f(p)是p的约数的个数,所以,要用到反素数,反素数的定义及求法。反素数s的约数个数比小于它的数的约数个数都多,最大反素数就是要找到的那个得到糖果最多的人。线段树记录的是还剩多少 人未出列。
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std; const int maxn = ;
char ch[maxn][] ;
int data[maxn] ; struct mode
{
int l,r ;
int value,sum ;
}tree[maxn * ] ;
//反素数表
int rev[] = {,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,};
//反素数对应的约数个数
int revs[] = {,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}; void build(int v,int l,int r)
{
tree[v].l = l ;
tree[v].r = r ;
tree[v].value = r-l+ ;
if(l == r) return ;
int mid = (l + r) >> ;
build(v*,l,mid) ;
build(v*+,mid+,r) ;
} int query(int v,int x)
{
tree[v].value-- ;
if(tree[v].l == tree[v].r) return tree[v].l ;
if(x <= tree[v*].value)
return query(v*,x) ;
else return query(v*+,x-tree[v << ].value) ;
}
int main()
{
int n, k;
while(scanf("%d %d",&n,&k) != EOF)
{
int i = ,maxx = ,p = ;
while(rev[i] <= n)
i++ ;
p = rev[i-] ;
maxx = revs[i-] ;
build(,,n ) ;
for(int j = ; j <= n ; j++)
scanf("%s %d",ch[j],&data[j]) ;
int yy ;
for(int i = ; i < p ; i++)
{
n -- ;
yy = query(,k) ;
if(n == ) break ;
if(data[yy] > )
k = (k-+data[yy]-) % n + ;
else k =( (k-+data[yy])%n+n) % n+ ;
}
printf("%s %d\n",ch[yy],maxx) ;
}
return ;
}
POJ 2886 Who Gets the Most Candies?(反素数+线段树)的更多相关文章
- (中等) 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?
题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...
- poj 2886 Who Gets the Most Candies?(线段树和反素数)
题目:http://poj.org/problem?id=2886 题意:N个孩子顺时针坐成一个圆圈且从1到N编号,每个孩子手中有一张标有非零整数的卡片. 第K个孩子先出圈,如果他手中卡片上的数字A大 ...
- POJ 2886 Who Gets the Most Candies? 线段树
题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ...
- POJ 2886.Who Gets the Most Candies? -线段树(单点更新、类约瑟夫问题)
线段树可真有意思呢续集2... 区间成段的替换和增减,以及区间求和等,其中夹杂着一些神奇的操作,数据离散化,简单hash,区间异或,还需要带着脑子来写题. 有的题目对数据的操作并不是直接按照题面意思进 ...
- POJ 2886 Who Gets the Most Candies? (线段树)
[题目链接] http://poj.org/problem?id=2886 [题目大意] 一些人站成一个圈,每个人手上都有一个数字, 指定从一个人开始淘汰,每次一个人淘汰时,将手心里写着的数字x展示 ...
- 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 位置的 孩子,他的 数字是 +num 那么因为他自己本身是要被踢走的,所以相对位置 为k= k+num-1 如果数字是 -num,那么按正着数就没影响,k=k-num.线段树存储当前 ...
- poj 2886 "Who Gets The Most Candies?"(树状数组)
传送门 参考资料: [1]:http://www.hankcs.com/program/algorithm/poj-2886-who-gets-the-most-candies.html 题意: 抢糖 ...
随机推荐
- ZooKeeper 分布式锁实现
1 场景描述 在分布式应用, 往往存在多个进程提供同一服务. 这些进程有可能在相同的机器上, 也有可能分布在不同的机器上. 如果这些进程共享了一些资源, 可能就需要分布式锁来锁定对这些资源的访问. 2 ...
- POJ3974 Palindrome
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- EF6调用存储过程,返回多个结果集处理
链接:http://www.codeproject.com/Articles/675933/Returning-Multiple-Result-Sets-from-an-Entity-Fram 案例: ...
- weblogic9.2重置密码
1.删除DefaultAuthenticatorInit.ldift 2.执行命令:java -cp /home/weblogic/bea/weblogic92/server/lib/weblogic ...
- Spring Mvc 笔记二之异常和文件上传
spring mvc的异常与文件上传 1.异常: spring注解版的异常有局部异常和全局异常 1.局部异常对单个controller有效;(在controller类写一 ...
- IF EXIST: The syntax of the command is incorrect.
Batch 脚本中使用 IF EXIST 语句时,总是提示 The syntax of the command is incorrect. 原始 bat 脚本如下: ECHO OFF SET proj ...
- compass sprite
1.compass init 初始化一个compass项目,并创建一个images文件夹用来存放合成的sprite图 2.将切好的小图放到images文件夹中 3.在sass文件夹中新建一个test. ...
- CentOS 配置Apache+Mysql+PHP (yum)与卸载
一.安装Apache2 #yum -y install httpd 安装配置完成,启动httpd服务#service httpd start 二.安装Mysql1.安装mysql#yum -y ins ...
- MySQL字符串中数字排序的问题
1.select * from table where 1 order by id*1 desc; 2.select * from table where 1 order by id+0 desc ...
- Config IIS server6.0-- HTTP 错误 500.21 - Internal Server Error 解决方案
HTTP 错误 500.21 - Internal Server Error 解决方案 不久前重新安装了Windows7,在安装了VS2010 开发平台之后,将网站发布到IIS,访问发 ...