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
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
解决方案:此题线段树用得比较简单,重点是一个反素数的理解和求解:
反素数:设g(x)为x的约数的个数,若对任意0<i<x,都有g(x)>g(i),则x为反素数;
反素数有如下性质:
1)质因子都是从2开始的连续素数
2)x=(2^t1)*(3^t2)*(5^t3)*(7^t4)*(11^t5)......,必然:t1>=t2>=t3>=t4>=t5....
我们根据这个性质求出在600000范围以内的反素数,然后就可以知道在1....N范围内的最大反素数,根据反素数,我们就很快求出到底是第几个出来的小孩能获得number最多。
以下是有以上性质求反素数:
code:当我们求的反素数以及它的约数的个数,就可以直接打表了。#include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<cmath> #define MMAX 500000 using namespace std; vector<int >prime; bool vis[MMAX]; long long primeMax,factMax; long long L,N; void find_prime() { memset(vis,false,sizeof(vis)); prime.clear(); for(int i=2; i<MMAX; i++) { if(!vis[i]) { vis[i]=true; prime.push_back(i); for(int j=i+i; j<MMAX; j+=i) vis[j]=true; } } } void get_Rprime(long long sum,long long k,long long cnt,int limits) { ///sum当前枚举到的数,k第k大的质因子,cnt该数约数的个数,limits质因子的个数上限 //cout<<" f"<<cnt<<endl; if(cnt>factMax) { primeMax=sum; factMax=cnt; //cout<<factMax<<endl; }///更新最大质因子个数 if(cnt==factMax&&primeMax<sum) { primeMax=min(primeMax,sum); }///若最大质因子个数相同,最优解更新为最小的 if(k>L) return ; long long temp=sum; for(int i=1; i<=limits; i++) { temp*=prime[k]; if(temp>N) break; get_Rprime(temp,k+1,cnt*(i+1),i);///累乘当前数,进行下一步搜索 } } int main() { freopen("out.txt","w",stdout); find_prime(); int len=prime.size(); long long temp=prime[0]; for(int i=1; i<len; i++) { temp*=prime[i]; if(temp>MMAX) { L=i; break; } }///求解要用到的不同素数的个数 // scanf("%d",&N); N=600000; while(N>0) { primeMax=0,factMax=0; get_Rprime(1,0,1,int(log2(N*1.0))); printf("(%lld,%lld)\n",primeMax,factMax); N=primeMax-1; } return 0; }
code:#include<iostream> #include<cstdio> #include<map> #define lson rt<<1,L,M #define rson rt<<1|1,M+1,R using namespace std; const int MMAX=500005; int cnt[MMAX<<2];///记录区间还有多少个孩子 int C[MMAX<<2];///记录孩子手上的编号 int N,K; char word[MMAX][12]; int num; int get; int RPrime[]= { //反素数 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, 554400 }; int fact[]= { //反素数约数个数 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,216 }; void build(int rt,int L,int R) { if(L==R) { scanf("%s %d",word[L],&C[rt]); cnt[rt]=1; } else { int M=(L+R)>>1; build(lson); build(rson); cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1]; } } void update(int rt,int L,int R,int k) { if(L==R) { get=C[rt]; cnt[rt]=0; num=L; // cout<<L<<endl; } else { int M=(L+R)>>1; if(k<=cnt[rt<<1]) { update(lson,k); } else if(k>cnt[rt<<1]) { update(rson,k-cnt[rt<<1]); } cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1]; } } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%d%d",&N,&K)) { //M build(1,1,N); int j=0; int m=0,s=1; while(1) { if(RPrime[j]>N) { m=fact[j-1]; s=RPrime[j-1]; break; } j++; }///先求1....N的范围内的反素数 int i=1; while(1) { update(1,1,N,K); if(i==s) break; if(N-i==0) break; if(get>0) { K=((K+get-1)%(N-i)+(N-i))%(N-i);///这个地方可随便搞搞 if(K==0) K=N-i; } else { K=((K+get)%(N-i)+N-i)%(N-i);///这个地方也是随便搞搞 if(K==0) K=N-i; } i++; } cout<<word[num]<<" "<<m<<endl; } return 0; }