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. UML类图图示样例

    下图来自<大话设计模式>一书:

  2. github  安装配置以及使用遇到的一些问题

    Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大成功,尤其是在Ruby社区中.目前,包括Rubinius ...

  3. 【0-1 背包模板】 poj 3624

    先看个未经优化的二维空间dp: #include <iostream> #include <cstdio> #include <cmath> #include &l ...

  4. Spring Boot 系列教程2-Data JPA

    Spring Data JPA 用来简化创建 JPA 数据访问层和跨存储的持久层功能. 官网文档连接 http://docs.spring.io/spring-data/jpa/docs/curren ...

  5. Hibernate 系列教程10-组成关系

    组成关系 在一个员工模型里面需要存入 员工公司所在地址的城市,街道 员工籍贯所在的城市,街道, 此时可以抽取城市,街道变成一个模型即是组成关系 Employee public class Employ ...

  6. POJ 2318 TOYS 叉积

    题目大意:给出一个长方形盒子的左上点,右下点坐标.给出n个隔板的坐标,和m个玩具的坐标,求每个区间内有多少个玩具. 题目思路:利用叉积判断玩具在隔板的左方或右方,并用二分优化查找过程. #includ ...

  7. php 四种基础算法 ---- 选择排序法

    2. 选择排序法: 选择排序法思路: 每次选择一个相应的元素,然后将其放到指定的位置 代码: function select_sort($arr) {//实现思路 双重循环完成,外层控制轮数,当前的最 ...

  8. UIGestureRecognizer 手势浅析

    目录[-] iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用 一.引言 二.手势的抽象类——UIGestureRecognizer 1.统一的初始化方法 2.手势状态 ...

  9. java 数组流

    Example10_10.java import java.io.*; public class Example10_10 { public static void main(String args[ ...

  10. storm的并发

    1 storm并行的基本概念 storm集群中的一个机器可以运行一个或者多个worker,对应于一个或者多个topologies. 1个worker进程运行1个或多个excutor线程.每个worke ...