POJ 2886 Who Gets the Most Candies? (线段树,单点更新)
来源:互联网 发布:淘宝购买伟哥 编辑:程序博客网 时间:2024/06/03 05:24
http://poj.org/problem?id=2886
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 2Tom 2Jack 4Mary -1Sam 1
Sample Output
Sam 3
Source
题意:
N个小朋友顺时针围成一圈玩约瑟夫,从第K个小朋友开始,这个小朋友出圈,如果他手上的号码x为正,那么他右边的第x个小朋友是下一个;x为负则他左边的第abs(x)个小朋友为下一个。如此直到全部出圈。第p个出圈的小朋友能获得F(p)个蜡烛,F(p)为p的约数的个数,求获得蜡烛最多的小朋友的名字和他的蜡烛数量。
分析:
突然发现原来我左右不分:顺时针为左,逆时针为右;
了解到一个叫做反素数的东东(OEIS A002182),对于x,记F(x)为x约数的个数,有k<x,F(k)<F(x);打表出反素数约数的个数(OEIS A002183);
线段树维护区间内剩余小朋友的个数,注意仔细推导左右移动的式子。
最后注意输出获得蜡烛最多的那个,所以我们先找到那个反素数,循环到这个数即可。
#include<cstdio>#include<iostream>#include<cstdlib>#include<algorithm>#include<ctime>#include<cctype>#include<cmath>#include<string>#include<cstring>#include<stack>#include<queue>#include<list>#include<vector>#include<map>#include<set>#define sqr(x) ((x)*(x))#define LL long long#define itn int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#define maxm#define maxn 500001using namespace std;int antiprime[]={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,554400};int factornum[]={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};char name[maxn][15];int number[maxn];int rest[maxn<<2];void build(int k,int l,int r){ rest[k]=r-l; if (r-l==1) return ; build(k*2+1,l,l+r>>1); build(k*2+2,l+r>>1,r);}int update(int res,int k,int l,int r){ rest[k]--; if (r-l==1) return l; if (res<rest[k*2+1]) { return update(res,k*2+1,l,l+r>>1); } else { return update(res-rest[k*2+1],k*2+2,l+r>>1,r); }}int query(int a,int b,int k,int l,int r){ if (b<=l || r<=a) return 0; if (a<=l && r<=b) return rest[k]; return query(a,b,k*2+1,l,l+r>>1)+query(a,b,k*2+2,l+r>>1,r);}int main(){ #ifndef ONLINE_JUDGE freopen("/home/fcbruce/文档/code/t","r",stdin); #endif // ONLINE_JUDGE int n,k,p; while (~scanf("%d %d",&n,&k)) { for (int i=0;i<n;i++) { scanf("%s %d",name[i],number+i); } build(0,0,n);k--; int pos=-1; while (antiprime[pos+1]<=n) pos++; for (int i=0;i<antiprime[pos];i++) { p=update(k,0,0,n); int rrest=query(0,p,0,0,n); int lrest=query(p,n,0,0,n); int total=lrest+rrest; if (total==0) break; int mov=number[p]%total; if (mov==0) mov=number[p]<0?1:-1; if (mov<0) { if (abs(mov)<=rrest) k=rrest+mov; else k=rrest+rrest+mov+lrest; } else { if (mov<=lrest) k=rrest+mov-1; else k=mov-lrest-1; } } printf("%s",name[p]); printf(" %d\n",factornum[pos]); } return 0;}
- 线段树单点更新+反素数 poj-2886-Who Gets the Most Candies
- POJ 2886 Who Gets the Most Candies?(线段树单点更新+反素数)
- poj 2886 Who Gets the Most Candies?(线段树单点更新模拟约瑟夫环)
- poj 2886 Who Gets the Most Candies?(线段树单点更新+反素数)真难。。。
- POJ 2886 Who Gets the Most Candies? (线段树,单点更新)
- poj 2886 Who Gets the Most Candies? (线段树单点更新应用)
- poj 2886 Who Gets the Most Candies?(线段树-单点更新)
- poj 2886 Who Gets the Most Candies? 【线段树单点更新 + 反素数】
- POJ 2886 Who Gets the Most Candies? [线段树-单点更新]【数据结构】
- POJ 2886 Who Gets the Most Candies? 线段树单点更新
- POJ2886:Who Gets the Most Candies?(线段树单点更新)
- Who Gets the Most Candies?(线段树+单点更新)
- POJ 2886 Who Gets the Most Candies?(单点更新)
- POJ 2886 Who Gets the Most Candies? 单点更新
- POJ 2886 Who Gets the Most Candies?【线段树单点更新+反素数打表】【好题】
- POJ 2886 Who Gets the Most Candies? 线段树
- poj 2886 Who Gets the Most Candies?(线段树#3)
- POJ 2886 Who Gets the Most Candies? 线段树
- 关于0x3f3f3f3f(0x四个3f)
- HDUJ 2553 N皇后问题 回溯法
- 线程同步
- spring 注解用法收集
- VS2010 添加OnInitDialog的方法
- POJ 2886 Who Gets the Most Candies? (线段树,单点更新)
- shell 匹配字符串 (eg.获得配置文件的值 )
- [iOS]UITableView实现简单瀑布流
- 转:typedef的用法
- C语言思想5
- HDU 4405 Aeroplane chess(概率dp,有传送的)
- 类方法学习五:getGenericSuperclass,getSuperclass
- 时间——日历
- error C2664: “inet_addr”: 不能将参数 1 从“TCHAR [128]”转换为“const char *”