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

时间:2023-03-08 23:56:17
(中等) 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?

  题意大致等同于约瑟夫环的问题,一次出来一个,然后出来x个人,其中x表示约数个数最多的数中最小的那个。

  做这个题时还不知道什么是反素数,用最笨的方法 (用了3s+时间) 打出了表,直接复制上的。然后就是构建线段树了,这个的线段树不难构建,就是记录区间内还没出去的人的个数。然后更新的话是标记某个点为出去了,并且返回这个点的位置,作为下一次计数的起点。询问的话就是某个区间的没出去的个数。

  反素数的话也在学习中,推荐ACdreamer的文章:http://blog.csdn.net/ACdreamers/article/details/25049767

代码如下:(注:写的比较乱,水平有限。)

#include<iostream>
#include<cstdio> using namespace std; const int remMax[][]={
{,},{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,},{,},
{,},{,},{,}}; int N,K;
char name[][];
int number[];
int BIT[*]; int findM(int x)
{
for(int i=;i<;++i)
if(remMax[i][]<=x&&remMax[i+][]>x)
return i;
} void pushUP(int po)
{
BIT[po]=BIT[po*]+BIT[po*+];
} void build_tree(int L,int R,int po)
{
BIT[po]=(R-L+); if(R==L) return; int M=(L+R)/;
build_tree(L,M,po*);
build_tree(M+,R,po*+);
} int query(int ql,int qr,int L,int R,int po)
{
if(ql>qr)
return ; if(ql<=L&&qr>=R)
return BIT[po]; int M=(L+R)/;
int temp=; if(ql<=M)
temp+=query(ql,qr,L,M,po*);
if(qr>M)
temp+=query(ql,qr,M+,R,po*+); return temp;
} int update(int un,int L,int R,int po)
{
--BIT[po]; if(L==R)
return L; int M=(L+R)/; if(BIT[po*]>=un)
return update(un,L,M,po*);
else
return update(un-BIT[po*],M+,R,po*+);
} int main()
{
int temp,temp1;
int n,las;
int times,fp; while(~scanf("%d %d",&N,&K))
{
for(int i=;i<=N;++i)
scanf("%s %d",name[i],&number[i]); build_tree(,N,); temp=findM(N);
times=remMax[temp][];
fp=remMax[temp][]; las=; for(int i=;i<=times;++i)
{
if(K>)
{
K%=(N-i+);
if(K==)
K=N-i+;
}
else
{
K%=(N-i+);
if(K==)
K=;
else
K=(N-i+)+K;
} temp1=query(las+,N,,N,); if(temp1>=K)
K=(N-i+)-(temp1-K);
else
K=(K-temp1); temp1=update(K,,N,); las=temp1;
K=number[las];
} printf("%s %d\n",name[las],fp);
} return ;
}