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

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 

题意:N个孩子顺时针坐成一个圆圈且从1到N编号,每个孩子手中有一张标有非零整数的卡片。第K个孩子先出圈,如果他手中卡片上的数字A大于零,下一个出圈的是他左手边第A个孩子。否则,下一个出圈的是他右手边第(-A)个孩子。第p个出圈的孩子会得到F(p)个糖果,F(p)为p的因子数。求得到糖果数最多的是哪个孩子及得到多少糖果。

思路:因为数据量很大,所以我们无法模拟,那么线段树有一种计算相对位置的应用,所以我们采用线段树,还有就是我们怎么求最大的约数个数的是那个,这个就要通过

反素数:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数.

最大约数个数的证明:在x以内,g(x)最大,在大于x小于n之间不存在因子数大于g(x)的数。假设存在p,使g(p)>g(x),则p就是n以内的最大反素数,与x使最大反素数矛盾,顾不存在p。

还有就是相对位置的移动这个比较难思考:因为我们要求余所以我们现将k减一,再加一来排除相对位置0的可能;当移动的数val>0的时候,因为他的消失会影响到后面的相对于第一个的位置,所以我们需要减一,而val<0,并不会影响到

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)
using namespace std;
const int maxn = 500005;

char name[maxn][11];
int val[maxn];
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};  

struct Node {
	int l, r, sum;
} node[maxn<<2];

void build(int l, int r, int rt) {
	node[rt].l = l;
	node[rt].r = r;
	node[rt].sum = r - l + 1;
	if (l == r)
		return;
	int m = l + r >> 1;
	build(l, m, lson(rt));
	build(m+1, r, rson(rt));
}

int query(int num, int rt) {
	node[rt].sum--;
	if (node[rt].l == node[rt].r)
		return node[rt].l;
	if (num <= node[lson(rt)].sum)
		return query(num, lson(rt));
	else query(num - node[lson(rt)].sum, rson(rt));
}

int main() {
	int n, k;
	while (scanf("%d%d", &n, &k) != EOF) {
		int cnt = 0, Max = 0, i = 0;
		while (a[i] <= n)
			i++;
		cnt = a[i-1];
		Max = b[i-1];
		build(1, n, 1);
		for (int i = 1; i <= n; i++)
			scanf("%s%d", name[i], &val[i]);
		int idx;
		for (i = 0; i < cnt; i++) {
			n--;
			idx = query(k, 1);
			if (n == 0)
				break;
			if (val[idx] > 0)
				k = (k-1+val[idx]-1) % n + 1;
			else k = ((k-1+val[idx]) % n + n) % n + 1;
		}
		printf("%s %d\n", name[idx], Max);
	}
	return 0;
}
  

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

时间: 2024-08-27 09:15:31

POJ - 2886 Who Gets the Most Candies? (反素数+线段树)的相关文章

POJ 2886 Who Gets the Most Candies? 反素数+线段树

题意:变形的约瑟夫环模型,每个人有一个数字a,从第K个人开始出列,如果数字是正的,就往后数a个人出列,如果书负数,就往反方向数. 然后用最基本的线段树处理约瑟夫环的方法即可 但是题目要求的是第x个出列的人的名字,x为1-N中约数最多的数中的最小的那个.这里需要求反素数,即不大于N约数最多的. 写起来比较多,容易写错,一开始连素数打表都写作了QAQ #include <cstdio> #include <iostream> #include <cstring> #incl

(中等) POJ 2886 Who Gets the Most Candies? , 反素数+线段树。

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 oth

poj 2886 Who Gets the Most Candies?(线段树+约瑟夫环+反素数)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9934   Accepted: 3050 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 o

poj 2886 Who Gets the Most Candies? (线段树单点更新应用)

poj 2886 Who Gets the Most Candies? 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 t

POJ 2886 Who Gets the Most Candies(线段树+约瑟夫环)

题目链接:POJ 2886 Who Gets the Most Candies [题目]N个孩子顺时针坐成一个圆圈,从1~N编号,每个孩子手中有一张标有非零整数的卡片.第K个孩子先出圈,如果他手中卡片上的数字A>0,下一个出圈的是他左手边第A个孩子.A<0,下一个出圈的是他右手边第(-A)个孩子.第p个出圈的孩子会得到F(p)个糖果,F(p)为p的因子数.输出得到糖果数最多的孩子的名字及糖果数目. [思路]孩子数目很大(1~500000),于是想到要用线段树来优化,然后就是模拟出圈过程.并更新

线段树(单点更新) POJ 2886 Who Gets the Most Candies?

题目传送门 1 #include <cstdio> 2 #include <cstring> 3 #define lson l, m, rt << 1 4 #define rson m+1, r, rt << 1 | 1 5 6 const int MAX_N = 500000 + 10; 7 int sum[MAX_N << 2]; 8 struct node 9 { 10 char name[15]; 11 int val; 12 }boy[

POJ 2886 Who Gets the Most Candies?(线段树模拟约瑟夫环,高合成数)

POJ 2886 Who Gets the Most Candies?(线段树模拟约瑟夫环,高合成数) ACM 题目地址:POJ 2886 Who Gets the Most Candies? 题意: N 个小孩围成一圈,他们被顺时针编号为 1 到 N.每个小孩手中有一个卡片,上面有一个非 0 的数字,游戏从第 K 个小孩开始,他告诉其他小孩他卡片上的数字并离开这个圈,他卡片上的数字 A 表明了下一个离开的小孩,如果 A 是大于 0 的,则下个离开的是左手边第 A 个,如果是小于 0 的, 则是

poj 2886 Who Gets the Most Candies?

Problem: 2886 User: shu_dayang Memory: 11104K Time: 1125MS Language: C++ Result: Accepted #include<iostream> #include<cstdio> #include<cstring> #define MID(a,b) ((a + b) >> 1) using namespace std; const int MAXN = 500005; int sum[M

POJ 2886 Who Gets the Most Candies?(线段树&#183;约瑟夫环)

题意  n个人顺时针围成一圈玩约瑟夫游戏  每个人手上有一个数val[i]   开始第k个人出队  若val[k] < 0 下一个出队的为在剩余的人中向右数 -val[k]个人   val[k] > 0 时向左数val[k]个  第m出队的人可以得到m的约数个数个糖果  问得到最多糖果的人是谁 约瑟夫环问题  n比较大 直接模拟会超时   通过线段树可以让每次出队在O(logN)时间内完成  类似上一道插队的题  线段树维护对应区间还有多少个人没出队  那么当我们知道出队的人在剩余人中排第几个