Description
Alice and Bob play the following game. A series of numbers is written on the blackboard. Alice and Bob take turns choosing one of the numbers, and replace it with one of its positive factor but not itself. The one who makes the product of all numbers become 1 wins. You can assume Alice and Bob are intelligent enough and Alice take the first turn. The problem comes, who is the winner and which number is Alice's first choice if she wins?
Input
This problem contains multiple test cases. The first line of each case contains only one number N (1<= N <= 100000) representing there are N numbers on the blackboard. The second line contains N integer numbers specifying the N numbers written on the blackboard. All the numbers are positive and less than or equal to 5000000.
Output
Print exactly one line for each test case. The line begins with "Test #c: ", where c indicates the case number. Then print the name of the winner. If Alice wins, a number indicating her first choice is acquired, print its index after her name, separated by a space. If more than one number can be her first choice, make the index minimal.
Sample Input
4 5 7 9 12 4 41503 15991 72 16057
Sample Output
Test #1: Alice 1 Test #2: Bob
Alice和Bob两个人在玩游戏,最开始有n个正整数,Alice先操作,之后双方轮流操作,可以任选一个数将其变为一个该数的一个和自己不等的约数,谁将所有数都变为1,谁就获胜了。
仔细分析,对于某一个数,他的后继有多少个呢,其实就是质因子的个数。比如说12,他的后继有6,4,2这3个。正好是他的质因子个数,2,2,3。相当于NIM游戏中,3个石头,每次可以拿走1-3个。
那我们打出素数表,然后暴力求出质因子个数就行了,也可以DP预处理质因子个数。
转换成NIM之后,考虑必胜的第一步策略。同样还是NIM一样。转换成拿走几个因子。
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> using namespace std; const int A=5000005; int prime[3000]; bool use[3000]; int sg[A]; int m[100005]; int pos; int getsg(int j) { if(sg[j]!=-1) return sg[j]; sg[j]=0; int jj=j; for(int i=0; i<pos&&prime[i]*prime[i]<=jj; i++) { int k=0; while(jj%prime[i]==0) { k++; jj/=prime[i]; } sg[j]+=k; } if(jj!=1)sg[j]++; return sg[j]; } int main() { memset(sg,-1,sizeof(sg)); memset(use,0,sizeof(use)); for(int i=2; i<sqrt(A+1.0); i++) { if(!use[i]) for(int j=i*2; j<sqrt(A+1.0); j+=i) { use[j]=true; } } pos=0; for(int i=2; i<sqrt(A+1.0); i++) { if(!use[i]) prime[pos++]=i; } int n,t=0; while(~scanf("%d",&n)) { t++; int tp=0; for(int i=1; i<=n; i++) { scanf("%d",&m[i]); int tpsg=getsg(m[i]); tp^=tpsg; } if(tp) { for(int i=1; i<=n; i++) if((tp^sg[m[i]])<sg[m[i]]) { printf("Test #%d: Alice %d\n",t,i); break; } } else { printf("Test #%d: Bob\n",t); } } }