ZOJ

A Game Between Alice and Bob

zoj-3529

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
题意:
  两个人博弈,给出n个数,每次可把一个数变成其因子,直到所有为1,无法再变者输。
题解:
  因为每一个数都可以写成N=p1^num1*p2^num2*...*pn^numn(pi为素数)的形式,每次变成一个因数,就相当于取走一个或者多个pi,所以每一个number就相当于Nim中的有(num1+num2+...numn)个石头,这样就变成了裸了Nim游戏
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=5e6+100;
 7 int sg[maxn];
 8 int NIM[maxn];
 9 bool isprime[maxn];
10 int prime[maxn],top;
11 void init(){
12     memset(sg,-1,sizeof(sg));
13     sg[1]=0;
14     top=0;
15     for(int i=2;i<maxn;i++){
16         if(!isprime[i]){
17             prime[top++]=i;
18             sg[i]=1;
19             for(int j=i+i;j<=maxn;j+=i)
20                 isprime[j]=true;
21         }
22     }
23 }
24 int getsg(int num){
25     if(sg[num]!=-1) return sg[num];
26     int ans=0,temp=num;
27     for(int i=0;prime[i]*prime[i]<=temp;i++)
28         while(temp%prime[i]==0){
29             ans++;
30             temp/=prime[i];
31         }
32     if(temp!=1)
33         ans++;
34     return sg[num]=ans;
35 }
36 void slove(){
37     int n;
38     int ans,ti=1;
39     while(~scanf("%d",&n)){
40         ans=0;
41         for(int i=1;i<=n;i++){
42             scanf("%d",&NIM[i]);
43             ans^=getsg(NIM[i]);
44         }
45         printf("Test #%d: ",ti++);
46         if(ans==0)
47             printf("Bob
");
48         else {
49             printf("Alice ");
50             for(int i=1;i<=n;i++)
51                 if((ans^sg[NIM[i]])<sg[NIM[i]]){
52                     printf("%d
",i);
53                     break;
54                 }
55         }
56     }
57 }
58 int main(void){
59     init();
60     slove();
61     return 0;
62 }