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?

  题意大致等同于约瑟夫环的问题,一次出来一个,然后出来x个人,其中x表示约数个数最多的数中最小的那个。

  做这个题时还不知道什么是反素数,用最笨的方法 (用了3s+时间) 打出了表,直接复制上的。然后就是构建线段树了,这个的线段树不难构建,就是记录区间内还没出去的人的个数。然后更新的话是标记某个点为出去了,并且返回这个点的位置,作为下一次计数的起点。询问的话就是某个区间的没出去的个数。

  反素数的话也在学习中,推荐ACdreamer的文章:http://blog.csdn.net/ACdreamers/article/details/25049767

代码如下:(注:写的比较乱,水平有限。)

#include<iostream>
#include<cstdio> using namespace std; const int remMax[][]={
{,},{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,}}; int N,K;
char name[][];
int number[];
int BIT[*]; int findM(int x)
{
for(int i=;i<;++i)
if(remMax[i][]<=x&&remMax[i+][]>x)
return i;
} void pushUP(int po)
{
BIT[po]=BIT[po*]+BIT[po*+];
} void build_tree(int L,int R,int po)
{
BIT[po]=(R-L+); if(R==L) return; int M=(L+R)/;
build_tree(L,M,po*);
build_tree(M+,R,po*+);
} int query(int ql,int qr,int L,int R,int po)
{
if(ql>qr)
return ; if(ql<=L&&qr>=R)
return BIT[po]; int M=(L+R)/;
int temp=; if(ql<=M)
temp+=query(ql,qr,L,M,po*);
if(qr>M)
temp+=query(ql,qr,M+,R,po*+); return temp;
} int update(int un,int L,int R,int po)
{
--BIT[po]; if(L==R)
return L; int M=(L+R)/; if(BIT[po*]>=un)
return update(un,L,M,po*);
else
return update(un-BIT[po*],M+,R,po*+);
} int main()
{
int temp,temp1;
int n,las;
int times,fp; while(~scanf("%d %d",&N,&K))
{
for(int i=;i<=N;++i)
scanf("%s %d",name[i],&number[i]); build_tree(,N,); temp=findM(N);
times=remMax[temp][];
fp=remMax[temp][]; las=; for(int i=;i<=times;++i)
{
if(K>)
{
K%=(N-i+);
if(K==)
K=N-i+;
}
else
{
K%=(N-i+);
if(K==)
K=;
else
K=(N-i+)+K;
} temp1=query(las+,N,,N,); if(temp1>=K)
K=(N-i+)-(temp1-K);
else
K=(K-temp1); temp1=update(K,,N,); las=temp1;
K=number[las];
} printf("%s %d\n",name[las],fp);
} return ;
}

(中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。的更多相关文章

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

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

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

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

  3. poj 2886 Who Gets the Most Candies?(线段树和反素数)

    题目:http://poj.org/problem?id=2886 题意:N个孩子顺时针坐成一个圆圈且从1到N编号,每个孩子手中有一张标有非零整数的卡片. 第K个孩子先出圈,如果他手中卡片上的数字A大 ...

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

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

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

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

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

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

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

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

  8. POJ 2886 Who Gets the Most Candies?

    思路: 对于 k 位置的 孩子,他的 数字是 +num 那么因为他自己本身是要被踢走的,所以相对位置 为k= k+num-1 如果数字是 -num,那么按正着数就没影响,k=k-num.线段树存储当前 ...

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

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

随机推荐

  1. 门面模式(Facade)解析

    门面模式使用一个门面类来包装一些复杂的类,对外提供一个简单的访问方法. 见如下代码: class CPU { public void startup() { System.out.println(&q ...

  2. 转载 Deep learning:五(regularized线性回归练习)

    前言: 本节主要是练习regularization项的使用原则.因为在机器学习的一些模型中,如果模型的参数太多,而训练样本又太少的话,这样训练出来的模型很容易产生过拟合现象.因此在模型的损失函数中,需 ...

  3. 购物车CheckBox全选、反选

    注意:不是很完美 //--------------------主布局文件--------------------------------- <LinearLayout xmlns:android ...

  4. 《JavaScript高级程序设计》读书笔记 ---变量

    ECMAScript 的变量是松散类型的,所谓松散类型就是可以用来保存任何类型的数据.换句话说,每个变量仅仅是一个用于保存值的占位符而已.定义变量时要使用var 操作符(注意var 是一个关键字),后 ...

  5. HDU 1681 Frobenius(完全背包+标记装满)

    一个完全背包,数组两百万,暴力可过 #include<iostream> #include<cstdio> #include<cstring> using name ...

  6. 首页商品图片显示错位,easy-popular批量上传

    =============关于zencart批量商品管理的说明================== 首先,安装好批量商品管理模块,设置 /tempEP 目录可写二.确认你已经在后台增加了一些分类目录. ...

  7. JS中substr和substring的用法和区别

    substr 和 substring都是JS 截取字符串函数,两者用法很相近,下面是两者的语法很示例: substr 方法 返回一个从指定位置开始的指定长度的子字符串.stringvar.substr ...

  8. Struts2 语法--result type

    result type: dispatcher,redirect:只能跳转到jsp,html之类的页面,dispatcher属于服务器跳转, redirect属于客户端跳转 chain: 等同于for ...

  9. Memcached函数整理

    public bool Memcached::add ( string $key , mixed $value [, int $expiration ] )  向key中添加值,如果key存在,返回f ...

  10. Linux 部署 Tomcat和JDK

    一:安装jdk下载将jdk加压后放到/usr/local目录下: [root@master ~]#chmod 755 jdk-6u5-linux-x64.bin [root@master ~]# ./ ...