【POJ 2886】 Who Gets the Most Candies?(反素数求最大因子数+线段树)

【POJ 2886】 Who Gets the Most Candies?(反素数求最大因子数+线段树)

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 12647 Accepted: 3937
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 ≤ KN) 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


很好的一个题 约瑟夫环的延伸 给定人数n 起点k

不同的是 偏移的距离根据此人携带数值而定 x表示从该处顺时针第x个人 -x表示逆时针

这样就可以用线段树 表示编号[L,R]区间剩余人数 通过遍历 可以较快找到当前第x个人


更进一步的 要求求出的是出对的几个人中得分最高的 每个人的得分定义为p的因子数 p表示此人第p个出队


这样就需要求出1~n 因子数最多的数和他的因子数

预处理 然而暴力的话就会超时 这里涉及到一个反素数


看别人帖子一直都看不太明白 问了问金巨还有楠神 豁然开朗 反素数是一类数 这类数的特征就是对于所有小于它的数 因子数都小于它的因子数 也就是说它之前的数的因子数都小于它的因子数


这样就好办了有木有 其实这个反素数不是重点 重点就是把程序中预处理的那一步转换成打表。。。前缀也想到了 就是打表这步 万万没想到啊。。。


代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

int n,k,pos;
char name[500500][11];
int to[500500];
int val[500500];
int id[500500];

int s[40] = {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[40] = {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 init()
{
	pos = 0;
	for(int i = 1; i <= 500000; ++i)
	{
		if(i >= s[pos]) ++pos;
		id[i] = s[pos-1];
		val[i] = b[pos-1];
	}
}

int bit[2333333];

void tree_init(int root,int l,int r)
{
	if(l == r)
	{
		bit[root] = 1;
		return;
	}
	int mid = (l+r)>>1;
	tree_init(root<<1,l,mid);
	tree_init(root<<1|1,mid+1,r);
	bit[root] = bit[root<<1]+bit[root<<1|1];
}

int Cut(int root,int l,int r,int ned)
{
	//printf("%d %d %d\n",l,r,bit[root]);
	bit[root]--;
	if(l == r)
	{
		pos = l;
		return 1;
	}
	int mid = (l+r)>>1;
	
	if(bit[root<<1] >= ned) return Cut(root<<1,l,mid,ned);
	return Cut(root<<1|1,mid+1,r,ned-bit[root<<1])+bit[root<<1];
}

int main()
{
	//fread();
	//fwrite();

	init();

	while(~scanf("%d%d",&n,&k))
	{
		for(int i = 1; i <= n; ++i)
			scanf("%s%d",name[i],&to[i]);
		tree_init(1,1,n);

		for(int i = 1; i <= id[n]; ++i)
		{
			k = Cut(1,1,n,k);
			if(i == id[n]) break;

			if(to[pos] > 0) --k;
			k = ((k+to[pos])%(n-i)+(n-i))%(n-i);
			if(!k) k = n-i;
			//printf("%d %d\n",pos,k);
		}
		printf("%s %d\n",name[pos],val[n]);
	}

	return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值