Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove
There are n piles of stones. Each time you select a pile, replace the quantity with the original factor. If all values are 1, it ends.
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4464
At first glance, In the SG game, the successor of a state is all factors, then the Mex operation, decisive TLE. However, the idea is still correct.
The following is the Tle code.
#include<iostream>#include<cstdio>#include<ctime>#include<cstring>#include<algorithm>#include<cstdlib>#include<vector>#define C 240#define TIME 10#define inf 1<<25#define LL long longusing namespace std;int sg[5000005];int get_sg(int n){ if(sg[n]!=-1) return sg[n]; int vis[1005]; memset(vis,0,sizeof(vis)); vis[0]=1; for(int i=2;i*i<=n;i++){ if(n%i==0){ vis[get_sg(i)]=1; vis[get_sg(n/i)]=1; } } for(int i=0;;i++) if(vis[i]==0) return sg[n]=i;}int main(){ int n,cas=0,a[100005]; memset(sg,-1,sizeof(sg)); sg[1]=0; for(int i=5000000;i;i--){ if(sg[i]==-1) get_sg(i); } while(scanf("%d",&n)!=EOF){ int ret=0; for(int i=0;i<n;i++){ scanf("%d",&a[i]); ret^=sg[a[i]]; } printf("Test #%d: ",++cas); if(ret==0) puts("Bob"); else{ printf("Alice "); for(int i=0;i<n;i++){ if(a[i]>(ret^sg[a[i]])){ printf("%d\n",i+1); break; } } } } return 0;}
After careful analysis, the number of successors of a certain number is actually the number of quality factors.
For example, 12 is followed by 6, 4, and 2. It is exactly the number of its quality factors, which are 2, 2, and 3. It is equivalent to 3 stones in a nim game. One to three stones can be taken at a time.
Then we can output the prime number table, and then obtain the number of quality factors by brute force. We can also pre-process the number of quality factors by DP.
After converting to Nim, consider the first step of winning strategy. The same is true for Nim. Convert to take away several factors.
#include<iostream>#include<cstdio>#include<ctime>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>#include<vector>#define C 240#define TIME 10#define inf 1<<25#define LL long longusing namespace std;bool flag[5000005]={0};int cnt=0,prime[5000000];int sg[5000005];void Prime(){ // prime[cnt++]=1; for(int i=2;i<=sqrt(1.0+5000000);i++){ if(flag[i]) continue; prime[cnt++]=i; sg[i]=1; for(int j=2;j*i<=sqrt(1.0+5000000);j++) flag[i*j]=true; }}int get_sg(int n){ if(sg[n]!=-1) return sg[n]; int k=0,t=n; for(int i=0;i<cnt&&prime[i]*prime[i]<=n;i++) if(n%prime[i]==0){ while(n%prime[i]==0){ k++; n/=prime[i]; } } if(n>1) k++; return sg[t]=k;}int main(){ memset(sg,-1,sizeof(sg)); sg[1]=0; Prime(); int n,cas=0,a[100000]; while(scanf("%d",&n)!=EOF){ int ret=0; for(int i=0;i<n;i++){ scanf("%d",&a[i]); ret^=get_sg(a[i]); } printf("Test #%d: ",++cas); if(ret==0) puts("Bob"); else{ printf("Alice "); for(int i=0;i<n;i++) if(sg[a[i]]>(ret^sg[a[i]])){ printf("%d\n",i+1); break; } } } return 0;}