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);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本系统的研发具有重大的意义,在安全性方面,用户使用浏览器访问网站时,采用注册和密码等相关的保护措施,提高系统的可靠性,维护用户的个人信息和财产的安全。在方便性方面,促进了校园失物招领网站的信息化建设,极大的方便了相关的工作人员对校园失物招领网站信息进行管理。 本系统主要通过使用Java语言编码设计系统功能,MySQL数据库管理数据,AJAX技术设计简洁的、友好的网址页面,然后在IDEA开发平台中,编写相关的Java代码文件,接着通过连接语言完成与数据库的搭建工作,再通过平台提供的Tomcat插件完成信息的交互,最后在浏览器中打开系统网址便可使用本系统。本系统的使用角色可以被分为用户和管理员,用户具有注册、查看信息、留言信息等功能,管理员具有修改用户信息,发布寻物启事等功能。 管理员可以选择任一浏览器打开网址,输入信息无误后,以管理员的身份行使相关的管理权限。管理员可以通过选择失物招领管理,管理相关的失物招领信息记录,比如进行查看失物招领信息标题,修改失物招领信息来源等操作。管理员可以通过选择公告管理,管理相关的公告信息记录,比如进行查看公告详情,删除错误的公告信息,发布公告等操作。管理员可以通过选择公告类型管理,管理相关的公告类型信息,比如查看所有公告类型,删除无用公告类型,修改公告类型,添加公告类型等操作。寻物启事管理页面,此页面提供给管理员的功能有:新增寻物启事,修改寻物启事,删除寻物启事。物品类型管理页面,此页面提供给管理员的功能有:新增物品类型,修改物品类型,删除物品类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值