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

来源:互联网 发布:淘宝购买伟哥 编辑:程序博客网 时间:2024/06/03 05:24

http://poj.org/problem?id=2886

Who Gets the Most Candies?
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 9426 Accepted: 2871Case 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

There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 500,000) and K (1 ≤ K ≤ N) on the first line. The next N lines contains the names of the children (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 2Tom 2Jack 4Mary -1Sam 1

Sample Output

Sam 3

Source

POJ Monthly--2006.07.30, Sempr


题意:

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;}


1 0
原创粉丝点击