Acdream 1112 Alice and BOB (SG function variants + prime sieve)

Source: Internet
Author: User

Test instructions: There are n number, Alice and Bob take turns to manipulate these numbers, if a number n=a*b and a>1,b>1, you can change that number to A and b two number;

Or can be reduced to a or B,alice first, ask who can win

Idea: First look at the division of each number of operations, we can know is actually divided by the number of elements or elements of the product of the factor

For example 70=2*5*7 we can become 10 (2*5) or 14 (2*7) or 35 (5*7) or 2 or 5 or 7 or 1 of these seven states

When we think of them (2,5,7) as 3 stones and a bunch of them, we actually do the Nim game with these stones.

I took a stone = "10 (2*5) I took the stone 7

14 (2*7) I took the stone. 5

35 (5*7) I took the stone. 2

I take two stones = "2" I took the stones. 5 and Gravel 7

5 I took the stone 2 and the stone 7

7 I took the stone 2 and the stone 5

I take three stones = "1" I took the stones. 2 and Stones 5 and Stones 7

Next we analyze to turn a number n=a*b into a and B, in fact, the idea here is very much like, take it as a gravel heap

I can divide into the first 10 (2*5) and 7

The second type 14 (2*7) and 5

Third Type 35 (5*7) and 2

On the whole, according to the unique decomposition theorem of positive integers, any positive integer x must have x= (P1^R1) * (P2^R2) *......* (PN^RN)

Define SUM=R1+R2+...+RN, the sum value is the total number of these stones, then sg=sg[sum1]^sg[susm2]^ ....

Is that the problem again? How should we ask for this sum?

We can get the minimum quality factor of each number by the prime sieve, we get a formula similar to recursion

The number of the mass factor of a positive integer = The number of the mass factor of (the positive integer/The minimum quality factor of this number) + 1 (that is, the number of the minimum mass factor is added 1)

And then the code will be implemented.

Code:

#include <iostream>#include<cstring>#include<cstdio>#defineMAXN 5000000using namespacestd;intprime[maxn+5],k=0, SAMLL[MAXN],SUM[MAXN];BOOLvisit[maxn+5];intsg[ the];voidGet_prime () {memset (visit,false,sizeof(visit)); memset (SAMLL,0,sizeof(SAMLL)); memset (SUM,0,sizeof(sum));  for(intI=2; i<=maxn;i++)    {        if(visit[i]==false) {prime[k++]=i;  for(intj=i+i;j<=maxn;j+=i) {visit[j]=true; if(samll[j]==0) samll[j]=i; } Samll[i]=i; }    }     for(intI=2; i<=maxn;i++) Sum[i]=sum[i/samll[i]]+1;}int Get(intN) {    if(sg[n]!=-1)returnSg[n]; BOOLvis[ the]; memset (Vis,false,sizeof(VIS));  for(intI=1; i<=n;i++) vis[Get(N-i)] =true;  for(intI=1; i<=n/2; i++) vis[Get(i) ^Get(N-i)] =true; intK;  for(intI=0;i< the; i++)    {        if(vis[i]==false)        {           returnsg[n]=i; }    }}intMain () {get_prime (); memset (SG,-1,sizeof(SG)); sg[0]=0; sg[1]=1;  for(intI=2; i<= -; i++)   {       Get(i); }   intN;  while(cin>>N) {intans=0;  for(intI=1; i<=n;i++)       {           intx; scanf ("%d",&x); Ans=ans^Sg[sum[x]]; }       if(ans) cout<<"Alice"<<Endl; Elsecout<<"Bob"<<Endl; }   return 0;}

Acdream 1112 Alice and BOB (SG function variants + prime sieve)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.