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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章