ZOJ - 3529 (数论+博弈SG)

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\n");
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\n",i);
53                     break;
54                 }
55         }
56     }
57 }
58 int main(void){
59     init();
60     slove();
61     return 0;
62 } 

 


转载于:https://www.cnblogs.com/1013star/p/10662754.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值