ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)

ZOJ 3529 - A Game Between Alice and Bob
Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu

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



题目大意:

给定n个数字,两个人轮流玩游戏,把这n个数字变成n的因子(不包含本身),最后全变成1就赢了,问你谁会赢,如果Alice赢了,把第一步选择的那一个数字的序号输出,如果有多种方案,输出序号小的。


解题思路:

sg[1]=0;
sg[2]=mex{sg[1]}=1;
sg[3]=mex{sg[1]}=1;
sg[4]=mex{sg[2],sg[1]}=2;
sg[5]=mex{sg[1]}=1;
sg[6]=mex{sg[2],sg[3]}=2;
sg[7]=mex{sg[1],sg[7]}=1;
sg[8]=mex{sg[1],sg[2],sg[4]}=3;
..........................
发现 sg[x]为 x因子的个数。
证明:因为 如果一个为a, 则:sg[x]=sg[(x/a)*a]=mex{sg[1]....,sg[(x/a)]}=sg[(x/a)]+1;

求出每个数字的sg后,只需要看sg的异或和,如果==0 和明显,输出Bob

否则,输出Alice,但是要输出Alice先走了哪一步,这里有用到了一个性质:只需要用合并后的SG值与每一堆SG值分别异或,看得到的结果是否小于原来该堆的SG值,如果小则可以取该堆。

这个性质暂时不是很懂,先用上,以后了解了会给出证明和过程。


解题代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

const int maxn=5000010;
int n,sg[maxn];
int d[110000];

vector <int> prime;
bool isprime[maxn];

void ini(){
    isprime[2]=true;
    for(int i=3;i<maxn;i+=2) isprime[i]=true;
    for(int i=3;i<maxn;i+=2){
        for(int j=i;j<maxn/i;j+=2){
            isprime[i*j]=false;
        }
    }
    for(int i=2;i<=maxn;i++){
        if(isprime[i]) prime.push_back(i);
    }
}

int SG(int x){
    if(sg[x]!=-1) return sg[x];
    int ret=0,size=prime.size(),tmp=x;
    for(int i=0;i<size && prime[i]*prime[i]<=x;i++){
        while(x%prime[i]==0){
            x/=prime[i];
            ret++;
        }
    }
    if(x>1) ret++;
    return sg[tmp]=ret;
}

int main(){
    ini();
    memset(sg,-1,sizeof(sg));
    int casen=0;
    while(scanf("%d",&n)!=EOF){
        int ans=0;
        for(int i=0;i<n;i++){
            scanf("%d",&d[i]);
            ans^=SG(d[i]);
        }
        if(ans==0) printf("Test #%d: Bob\n",++casen);
        else{
            for(int i=0;i<n;i++){
                if( ( ans^SG(d[i]) ) < SG(d[i]) ){
                    printf("Test #%d: Alice %d\n",++casen,i+1);
                    break;
                }
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炒饭君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值