poj2886 Who Gets the Most Candies?(反素数 + 线段树)

Who Gets the Most Candies?
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 11784   Accepted: 3688
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个孩子坐一圈,依次跳出圈子,跳出的孩子手中卡片上的数值v[i]表示他左手边(顺时针数)第v[i]个孩子跳出圈子。第p个跳出圈子的孩子得到的糖数等于p约数的个数,求问拿糖最多的孩子的名字和糖数。

这道题可以转化为求出1~n中约数个数最多的数p,并且求出第p个跳出圈子的孩子原本的位置。

因为敲这道题的关系,我发现了一个很神奇的东西,叫作“反素数”。以我的语文水平还不能完全清楚的表达反素数的含义,所以找了百度百科。反素数:对于任何正整数x,其约数的个数记做g(x)。例如g(1) = 1,g(6) = 4。如果某个正整数x满足:对于任意i(0 < i < x),都有g(i) < g(x),则称x为反素数。反素数打表就好,感觉一般到500,000就够了吧。

找出约数最多的数p,然后用线段树查询第p个孩子的原始位置。只是一个简单的线段树,感觉连pushdown、pushup都不需要。可能因为我脑子容易转不过来,觉得求跳出圈子孩子的当前序号的公式有点麻烦,推导了很久。因为涉及到对n取余,为了不出现得0的现象,将序号1~n全部减一变成0~n-1。

#include <cstdio>
#include <iostream>
#include <algorithm>
#define maxn 500005
#define ls node << 1
#define rs node << 1 | 1
#define lson l,mid,ls
#define rson mid + 1,r,rs

using namespace std;

char name[maxn][12];
int v[maxn];

struct btree {
    int l;
    int r;
    int sum;
} t[maxn << 2];

int a[37]= {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,500001
           };
int b[37]= {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,1314521};

void build(int l, int r, int node)
{
    t[node].l = l;
    t[node].r = r;
    t[node].sum = r - l + 1;
    if(l == r)
        return;
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
}

int query(int num, int node)
{
    t[node].sum --;
    if(t[node].l == t[node].r)
        return t[node].l;
    if(num <= t[ls].sum)
        return query(num,ls);
    else
        return query(num - t[ls].sum,rs);
}

int main()
{
    int n,k,i,p,x,m;
    while(scanf("%d%d",&n,&k) != EOF) {
        i = 0;
        while(a[i] <= n)
            i++;
        p = a[i - 1];
        m = b[i - 1];
        for(i = 1; i <= n; i++)
            scanf("%s%d",name[i],&v[i]);
        build(1,n,1);
        //printf("11111\n");
        //printf("p = %d\n", p);
        for(i = 0; i < p; i++) {
            x = query(k,1);
            //printf("x = %d, k = %d\n",x,k);
            n--;
            if(!n)
                break;
            if(v[x] > 0)//因为要对n取余,减1将1~n转化为0~n-1。
                k = (k - 1 - 1 + v[x]) % n + 1;//大于0则顺时针。找第k个顺时针方向的第v[x]个,相当于第k-1个顺时针方向的第v[x]个
            else
                k = ((k - 1 + v[x]) % n + n) % n + 1;//小于0,则逆时针,第一次取余保证值在-n~0之间。
        }
        //printf("22222\n");
        printf("%s %d\n",name[x],m);
    }
}


发布了39 篇原创文章 · 获赞 1 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章