poj 2886 Who Gets the Most Candies? 【线段树单点更新 + 反素数】

Who Gets the Most Candies?
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 12070 Accepted: 3771
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


题意:N个孩子顺时针坐成一个圆圈(编号从1到N),每个孩子手中有一张卡片(上有非0数字)。现在让第K个孩子先出圈,如果该孩子手中卡片上的数字A大于零,下一个出圈的是他左手边第A个孩子,否则,下一个出圈的是他右手边第(-A)个孩子。

第p个出圈的孩子会得到F(p)个糖果,F(p)为p的因子数。输出得到糖果数最多的孩子名字和得到的糖果数。


反素数:对于正整数x,其约数的个数记做g(x)。如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数。设p是不大于N的反素数,这样得到糖果数最多的孩子显然是第p个出圈的。


分析:假设当前出圈的是剩余孩子中的第K个,他手中的数字为A。

若A大于零,下一个出圈的是剩余孩子中的第(K-1+A-1)%n+1个;

若A小于零,下一个出圈的就应该是剩余孩子中的第((K-1+A)%n+n)%n+1个。

因此,我们只需要每次求出当前出圈孩子的位置并模拟这个过程p次或者N-1次。模拟过程中,最后一个出圈的孩子就是我们要找的孩子。



思路:用线段树求出当前出圈孩子的位置pos,这样就可以求出下一个出圈的是第几个孩子。

 


AC代码:


#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 500000+10
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
struct Tree
{
    int l, r;
    int sum;//记录当前区间 有多少个人
};
Tree tree[MAXN<<2];
int antiprime[]= //反素数
{
    0,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 primenum[]= //反素数约数个数
{
    0,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
};
struct Man
{
    char name[20];
    int card;
};
Man num[MAXN];
void PushUp(int o)
{
    tree[o].sum = tree[ll].sum + tree[rr].sum;
}
void build(int o, int l, int r)
{
    tree[o].l = l;
    tree[o].r = r;
    if(l == r)
    {
        tree[o].sum = 1;
        return ;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(o);
}
int query(int o, int pos)
{
    tree[o].sum--;//少一个人
    if(tree[o].l == tree[o].r)
        return tree[o].l;
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(pos <= tree[ll].sum)
        return query(ll, pos);
    else
        return query(rr, pos-tree[ll].sum);
}
int main()
{
    int N, K;
    while(scanf("%d%d", &N, &K) != EOF)
    {
        for(int i = 1; i <= N; i++)
            scanf("%s%d", num[i].name, &num[i].card);
        build(1, 1, N);
        int used = 1;
        while(antiprime[used] <= N) used++;
        used--;
        int time = antiprime[used];//模拟次数
        int ans = primenum[used];//最终结果
        int pos;
        for(int i = 1; i <= time; i++)//模拟
        {
            N--;//总人数减一
            pos = query(1, K);//得到上一次出队的人 的位置
            if(N == 0) break;
            int A = num[pos].card;//手中卡片 数字
            if(A > 0)
                K = (K - 1 + A - 1) % N + 1;//下一个出队 是第几个孩子
            else
                K = ((K - 1 + A) % N + N) % N + 1;
        }
        printf("%s %d\n", num[pos].name, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值