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

描述

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§ candies where F§ is the number of positive integers that perfectly divide p. Who gets the most candies?

输入

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 10^8) 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 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.

样例输入

4 2
Tom 2
Jack 4
Mary -1
Sam 1

样例输出

Sam 3

题意:

给定人数n,及他们的名字和卡片值,这n个人围城圈进行一场游戏,从第k个人开始,这个人出圈,并根据他的卡片值来确定下一个出圈的人,正值为顺时针,负为逆时针。并根据出圈顺序来确定每人得到的糖数,假设1_1第x个出圈,那么1_1得到的糖果数为x的因子数。求该游戏中获得糖果最多的人和他获得的数量,如果存在多个最大值,则输出最先出圈的。
思路:
由于获得糖果的数量只与出圈的顺序有关,与人的名字和卡片值无关,那么在得到人数n后糖果的最大值和他是第几个出圈的就已经可以确定了。所以只要确定那个出圈的人的名字就可以了。
要解决[1,n]内最大因子数的问题,先引入一下反素数的概念,设G(x)为x的因子数,如果 G(x)>G(i),(1<=i<x),那么 x就叫做反素数。很显然题目要求的[1,n]内最大因子数就是找该区间内的最大反素数。由于题目有多组数据,所以我选择先把[1,5e5]内的反素数预处理出来(后来由于反素数数量较少,直接手动输入了😂)。
最后就只剩下确定最大值出圈人的名字这一任务了,暴力模拟肯定不行,那么就回过头想一下,如果设第一个人的值为1,第二个人的值为2,第x个人的值为x(x<n),那么找第k个人的问题就转变成了求第k小的问题了这个问题可以用权值线段树在O( log n )内解决,那么总的复杂度为O(nlogn )满足要求。

代码

#include<stdio.h>
#include<algorithm>
using namespace std;
const int Ms=5e5+5;
struct node
{
    char s[11];
    int nex;
}peo[Ms];
int fprime[36]={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,555555};
int num[36]={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};
int T[Ms<<2];
void build(int l,int r,int root)
{
    if(l==r)
    {
        T[root]=1;
        return;
    }
    int m=(l+r)>>1;
    build(l,m,root*2);
    build(m+1,r,root*2+1);
    T[root]=T[root*2]+T[root*2+1];
}
int query(int l,int r,int k,int root)
{
    if(l==r)
    {
        T[root]=0;
        return l;
    }
    int m=(l+r)>>1,sum=T[root*2],ans;
    if(k<=sum)ans= query(l,m,k,root*2);
    else ans= query(m+1,r,k-sum,root*2+1);
    T[root]=T[root*2]+T[root*2+1];
    return ans;
}
int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%s%d",peo[i].s,&peo[i].nex);
        }
        build(1,n,1);
        int aim=0,id,key,ans;
        while(fprime[aim]<=n)aim++;
        ans=num[aim-1];
        aim=fprime[aim-1];
        k--;
        while(aim--)
        {
            key=query(1,n,k+1,1);
            if(aim==0)break;
            if(peo[key].nex>0)
            {
                id=(k-1+peo[key].nex)%T[1];
            }
            else
            {
                id=((k+peo[key].nex%T[1])+T[1])%T[1];
            }
            k=id;
        }
        printf("%s %d\n",peo[key].s,ans);
    }
    return 0;
}

若有什么错误,欢迎指正^ _ ^ 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值