博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ 2886] Who Gets the Most Candies?
阅读量:5283 次
发布时间:2019-06-14

本文共 3509 字,大约阅读时间需要 11 分钟。

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 2Tom 2Jack 4Mary -1Sam 1

Sample Output

Sam 3

Source

, Sempr
 
题解:
反素数+线段树(删除编号为k的元素)
反素数:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数(百度词条)·
tr数组记下每个结点所代表区间的长度,然后每次查找当前序列排名为k的元素在原序列中的编号
1 /* 2   挑战2-线段树 3   反素数+线段树(删除编号为k的元素) 4   by-solution 5 */ 6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #define ll long long13 #define ls x<<114 #define rs x<<1|115 using namespace std;16 17 const int N = 500010;18 19 int n,k,ans,tr[N*4];20 int a[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},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};21 22 struct Node {23 int v;24 char s[11];25 }q[N];26 27 int gi() {28 int x=0,o=1; char ch=getchar();29 while(ch!='-' && (ch<'0'||ch>'9')) ch=getchar();30 if(ch=='-') ch=getchar(),o=-1;31 while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();32 return o*x;33 }34 35 void build(int x, int l, int r) {36 tr[x]=r-l+1;37 if(l==r) return;38 int mid=(l+r)>>1;39 build(ls,l,mid),build(rs,mid+1,r);40 }41 42 int query(int x, int l, int r, int k) {43 tr[x]--;44 if(l==r) return l;45 int mid=(l+r)>>1;46 if(k<=tr[ls]) return query(ls,l,mid,k);47 else return query(rs,mid+1,r,k-tr[ls]);48 }49 50 int main() { //poj288651 while(~scanf("%d%d", &n, &k)) {52 int cnt=0,p,pos,i,n1=n;53 for(i=1; i<=n; i++) {54 scanf("%s%d", q[i].s, &q[i].v);55 }56 build(1,1,n);57 while(a[cnt]<=n) cnt++;58 ans=b[cnt-1],p=a[cnt-1];59 while(p--) {60 pos=query(1,1,n1,k),n--;61 if(!n) break;62 if(q[pos].v>=0) k=(k-1+q[pos].v-1)%n+1;//下一个要ti的人当前的编号,很妙~63 else k=((k-1+q[pos].v)%n+n)%n+1;//mod +mod mod,转圈圈的题算完答案先mod一发再说~64 }//第一个k-1意思是从当前第一个开始数,第二个-1意思是当前的数已经被删除,如果要往右数应从上一个数开始65 printf("%s %d\n", q[pos].s,ans);66 }67 return 0;68 }

 

 

转载于:https://www.cnblogs.com/HLXZZ/p/7471448.html

你可能感兴趣的文章
java生成验证码图片
查看>>
THREADSPOOL
查看>>
jira集成fisheye代码深度查看工具安装绿色版
查看>>
C#跨线程操作控件的最简单实现探究
查看>>
Ubuntu server12.04安装JDK+Tomcat+mysql
查看>>
brock pallet
查看>>
hihocoder--1384 -- Genius ACM (倍增 归并)
查看>>
NowCoder--牛客练习赛30 C_小K的疑惑
查看>>
C++中GB2312字符串和UTF-8之间的转换
查看>>
透视图扩展 Perspective Extensions
查看>>
espeak
查看>>
【VS开发】VC下加载JPG/GIF/PNG图片的两种方法
查看>>
一篮子苹果,每天吃一半多一个吃,第十天吃一半多一个后就剩余一个,求一共多少个苹果,JAVA版...
查看>>
css——display: flex之垂直方向布局的具体实践
查看>>
vue基础——路由懒加载
查看>>
Oracle sql优化示例
查看>>
sql-向已有数据的表添加约束
查看>>
Angularjs的核心概念
查看>>
通过用户模型,对数据库进行增删改查操作。
查看>>
everything 搜索文件
查看>>