2021“MINIEYE杯”中國大學生算法設計超級聯賽(8)1006. GCD Game(Nim博弈)


Problem Description

Alice and Bob are playing a game.

They take turns to operate. There are n numbers, a1 , a2 , ... , an. Every time, the player plays in 3 steps.
1.Arbitrarily chooses one number ai.
2.Arbitrarily chooses another number x(1≤x<ai).
\3. Replace the number ai with gcd(ai,x). Here, gcd(u,v) refers to the Greatest Common Divisor of u and v.

When a player can not make a single move he/she loses the game. Alice moves the first and she asks you to tell her who will win the game if both player play optimally.

Input

The first line contains a number T(1≤T≤100), the number of testcases.

For each testcase, there are two lines.
The first line contains one number n(1≤n≤106).
The second line contains n numbers a1 , a2 , ... , an(1≤ai≤107).

It is guaranteed that for all testcases, ∑n≤106.

Output

For each testcase, output the answer in one line. If Alice will win the game, print "Alice", otherwise print "Bob".

Sample Input

2
1
1
1
2

Sample Output

Bob
Alice

實際上這就是Nim博弈。首先注意到gcd的本質。不妨設\(a = p_1^{x_1}p_2^{x_2}...p_n^{x_n}, b = p_1^{y_1}p_2^{y_2}...p_n^{y_n}\),則\(gcd(x, y) = p_1^{min(x_1, y_1)}p_2^{min(x_2, y_2)}...p_n^{min(x_n, y_n)}\)考慮把每個ai唯一分解為若干個質數冪次的乘積。因為每次x是任意選的,且\(gcd(a_i, x)\)是ai的因子,所以當前玩家可以直接選擇當前ai的任意因子,這樣很自然的就可以把初始ai的質因子累積個數看做石子數,ai變成1則不能操作等價於此時ai的每個質因子次數為0,也就是石子數為0。用歐拉篩預處理出每個數的質因子個數,對於輸入直接查詢sg值然后求異或和進行普通nim博弈即可。

#include<bits/stdc++.h> 
using namespace std;
const int maxn=1e7+10;
int sg[maxn],primes[maxn];
int m=0; 
void getprimes(){
    sg[1]=0;
    for(int i=2;i<=maxn;i++){
        sg[i]=1;
    }
    for(int i=2;i<=maxn;i++){
        if(sg[i]==1) {sg[i]=1;primes[++m]=i;}
        for(int j=1;j<=m&&primes[j]*i<=maxn;j++){
            sg[primes[j]*i]=sg[i]+1;
            if(i%primes[j]==0)break;
        }
    }
}
int main(){
    getprimes();
    int T;
    cin>>T;
    while(T--){
        int n,x;
        cin>>n;
        int ans=0;
        int one=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            if(x==1) one++;
            ans^=sg[x];
        }
        if(one==n) puts("Bob");
        else if(ans==0) puts("Bob");
        else puts("Alice");
    }
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM