您的位置:首页 > 其它

POJ 2886 Who Gets the Most Candies?

2014-11-17 15:13 302 查看
题目链接:http://blog.csdn.net/qingniaofy/article/details/7749281

Who Gets the Most Candies?

Time Limit: 5000MSMemory Limit: 131072K
Total Submissions: 10608Accepted: 3300
Case 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 2
Tom 2
Jack 4
Mary -1
Sam 1

Sample Output
Sam 3

Source

POJ Monthly--2006.07.30, Sempr

题目:N 个小孩围成一圈,他们被顺时针编号为 1 到 N。每个小孩手中有一个卡片,上面有一个非 0 的数字,游戏从第 K 个小孩开始,他告诉其他小孩他卡片上的数字并离开这个圈,他卡片上的数字 A 表明了下一个离开的小孩,如果 A 是大于 0 的,则下个离开的是左手边第 A 个,如果是小于 0 的,则是右手边的第 -A
个小孩。游戏将直到所有小孩都离开,在游戏中,第 p 个离开的小孩将得到 F(p) 个糖果,F(p) 是 p 的约数的个数,问谁将得到最多的糖果。输出最幸运的小孩的名字和他可以得到的糖果。

算法:先求出反素数,然后用线段树维护区间总数以模拟类似于约瑟夫环的操作。

为什么要用到反素数?

反素数的定义:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数·--来自百度百科

显然:小于n的最大反素数的约数个数最多。利用反素数的定义结合反证法即可证明。
首先,我们要求出反素数,此题只需要求出500000以内的反素数即可。因为500000以内的反素数还是比较少的,我们可以先在本地求出来,然后打表。没必要设计一个高效的算法求反素数,因为我们在本地求反素数并不需要考虑时间复杂度,但是还是要比较快求出来,不可能让程序跑个一两天的吧。
下面是本地求反素数的算法
1,.筛出1000000以内的素数,并存下来,即将素数个数打表,这是为了快速分解质因数。
2.分解质因数,分解成n=a1^b1*a2^b2*......*an^bn,那么n的约数的个数为g(n)=(b1+1)*(b2+1)*...*(bn+1)。
3.求出1000000以内数的约数个数,并存下来,即将约数个数打表。这是为了后面快速判断反素数
4.对于1~1000000以内的数x,枚举1~x-1之间的数y,如果都满足g(y)<g(x),那么很显然,x就是反素数。
下面给出实现的算法
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<list>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int L=600005;
int prime[L],sum,ans[L],prePrime[L];
bool is[L];
void init()
{
    fill(is,is+L,1);
    is[0]=0;is[1]=0;
    int np=0;
    for(int i=2;i<L;i++)
    if(is[i])
    {
        prime[++np]=i;
        for(int j=2*i;j<L;j+=i) is[j]=0;
    }
   // for(int i=1;prime[i]<=100;i++)
          //  printf("%d\n",prime[i]);
}
int factor_n(int n)
{
    int pos=1,cnt;
    sum=1;
    while(prime[pos]*prime[pos]<=n)
    {
        cnt=1;
        while(n%prime[pos]==0)
        {
            cnt++;
            n/=prime[pos];
        }
        sum*=cnt;
        cnt=1;
        pos++;
    }
    if(n!=1) sum*=2;
    return sum;
}
void getYueShu()
{
    for(int i=1;i<L;i++)
    {
        ans[i]=factor_n(i);
       // printf("sum %d = %d\n",i,ans[i]);
    }
}
void getprPrime()
{
    int np=0;
    for(int i=1;i<L;i++)
    {
        bool flag=1;
        for(int j=1;j<i;j++)
        {
            if(ans[j]>=ans[i])
            {
                flag=0;
                break;
            }
        }
        if(flag) prePrime[++np]=i,printf("%d\n",i);
    }
}
void print()
{
    //for(int i=1;prePrime[i]<L;i++)
      //  printf("%d\n",prePrime[i]);
}
int main()
{
    init();
    getYueShu();
    getprPrime();
    print();
    return 0;
}


求出600000以内的反素数花了1.7s,感觉速度还行。

好,我们把求出来的反素数直接打表。
接下来就是利用线段树的单点更新维护区间的点的个数模拟类似于约瑟夫问题的过程,这就比较简单了。

对于 k 位置的 孩子,他的 数字是 +num 那么因为他自己本身是要被踢走的,所以相对位置 为k= k+num-1

如果数字是 -num,那么按正着数就没影响,k=k-num。线段树存储当前区间共有多少个人,每一次找到第k (前面有k-1个)个孩子,经过的区间都要 减1,然后记录被踢走的孩子编号。query和update放在一个函数上实现。

下面是AC代码。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<vector>
#include<map>
#include<list>
#include<set>
#include<queue>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
using namespace std;
const int maxn=500005;
int n,k;
int sum[maxn<<2],value[maxn];
char name[maxn][15];
int RPrime[]={//反素数
    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 fact[]={//反素数约数个数
    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,216
};

void PushUp(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(rt);
}
int query(int x,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=0;
        return l;
    }
    int m=(l+r)>>1;
    int ans=(x<=sum[rt<<1]?query(x,lson):query(x-sum[rt<<1],rson));
    PushUp(rt);
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=1;i<=n;i++) scanf("%s%d",name[i],&value[i]);
        build(1,n,1);
        int pos=0,cnt=0;
        while(RPrime[cnt]<=n) cnt++;cnt--;
        value[0]=0;
        for(int i=0;i<RPrime[cnt];i++)
        {
            if(value[pos]>0)
                k=((k+value[pos]-2)%sum[1]+sum[1])%sum[1]+1;
            else
                k=((k+value[pos]-1)%sum[1]+sum[1])%sum[1]+1;
            pos=query(k,1,n,1);
        }
        printf("%s %d\n",name[pos],fact[cnt]);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: