POJ 2886 线段树单点更新+数论

Who Gets the Most Candies?
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 12514 Accepted: 3906
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。接着按照这个小孩纸片上的数字k往下数第k个人(k为正则顺时针数,为负则逆时针)出队,得到编号2。依次继续直到所有人都出队。问那个小孩得到的编号的因子数最多,如果有多个最多的,输出第一个小孩。

题解

1.先将每个孩子都标上编号,这个类似之前排队的操作。我们用一个线段树维护,按照题目给的规则给孩子编号。由于是个圈所以要进行一个模操作。
2.孩子编好号后接着就是找每个数的因子数了。网上有人说用反素数来做,但是本巨蒻实在是不会反素数来做。就先用埃式筛法找出所有素数,然后根据一个数的素数分解原理。狄推出每一个数的因子数。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <sstream>
#include <vector>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)

using namespace std;
#define SIZE 500000+50

char name[SIZE][15];
int num[SIZE];
int ans[SIZE];
bool pri[SIZE];
int pp[SIZE];
int T[1048576+50];
int N,Move;
int cnt[SIZE];

void init(int n){
    int i,j = 0;
    N = 1;
    while(N<n){
        N<<=1;
        j++;
    }
    int k = j,p = 1;
    f(i,0,k)
        f(j,1,1<<i){
            T[p++] = 1<<(k-i);
        }
}
//前面有k个空位,p在第k+1个位置
void addPoint(int p,int k,int t,int l,int r){
    if(l==r){
        ans[t-(N-1)] = p;
        Move = num[t-(N-1)];
        T[t] = 0;
        return ;
    }
    int m = (l+r)>>1;
    if(T[t<<1]>k) addPoint(p,k,t<<1,l,m);
    else addPoint(p,k-T[t<<1],t<<1|1,m+1,r);
    T[t] = T[t<<1]+T[t<<1|1];
}

void ai(){
    int i,j,k = 0;
    m0(pri);
    pri[1] = 1;
    f(i,2,SIZE-2){
        if(!pri[i]){
            pp[k++] = i;
            for(j = i*2;j<=SIZE-2;j+=i)
                pri[j] = 1;
        }
    }
    m0(cnt);
    N = k;
    cnt[1] = 1;
    cnt[2] = 2;
    k = 2;
    for(i = 3;i<=SIZE-1;i++){
        if(!pri[i]){
            cnt[i] = 2;
            k = i;
        }else{
            for(j = 0;j<N;j++){
                    if(i%pp[j]==0){
                        int sum = 0,tem = i;
                        while(tem%pp[j]==0){
                            tem/=pp[j];
                            sum++;
                        }
                        cnt[i] = (sum+1)*cnt[tem];
                        break;
                    }
            }
        }
    }
}

int main()
{
//  ios_base::sync_with_stdio(false); cin.tie(0);
//    freopen("data.out","w",stdout);
    int n,be;
    char c;
    int i;
    ai();
    while(~scanf("%d%d",&n,&be)){

        init(n);

        f(i,1,n)scanf("%s%d",name[i],&num[i]);

        int s,M = n;
        f(i,1,n){
            if(i==1){
                s = be%M;
                if(s==0) s = M;
                addPoint(i,s-1,1,1,N);
                M--;
            }else{
                if(Move>0){
                    int e = (s-2+Move)%M;
                    addPoint(i,e,1,1,N);
                    s = e+1;
                    M--;
                }else{
                    int e = s-1+Move;
                    while(e<0)
                        e+=M;
                    e%=M;
                    addPoint(i,e,1,1,N);
                    s = e+1;
                    M--;
                }
            }
        }
        int Max = -1,Ma = -1;
        f(i,1,n){
            int tem = cnt[ans[i]];
            if(tem>Max){
                Max = tem;
                Ma = i;
//            是先最大数相同时第一个出队的输出
            }else if(tem==Max){
                if(ans[i]<ans[Ma])
                    Ma = i;
            }

        }
        cout<<name[Ma]<<" "<<Max<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值