Educational Codeforces Round 169 (Rated for Div. 2) E

CF2004E Not a Nim Problem

题目:

Two players, Alice and Bob, are playing a game. They have n n n piles of stones, with the i i i-th pile initially containing a i a_i ai stones.

On their turn, a player can choose any pile of stones and take any positive number of stones from it, with one condition:

  • let the current number of stones in the pile be x x x. It is not allowed to take from the pile a number of stones y y y such that the greatest common divisor of x x x and y y y is not equal to 1 1 1.

The player who cannot make a move loses. Both players play optimally (that is, if a player has a strategy that allows them to win, no matter how the opponent responds, they will win). Alice goes first.

Determine who will win.
Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

Each test case consists of two lines:

  • the first line contains a single integer n n n ( 1 ≤ n ≤ 3 ⋅ 1 0 5 1 \le n \le 3 \cdot 10^5 1n3105);
  • the second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 7 1 \le a_i \le 10^7 1ai107).

Additional constraint on the input: the sum of n n n across all test cases does not exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3105.
Output

For each test case, output Alice if Alice wins, or Bob if Bob wins.

分析:

令石子数为 x x x ,则:
x x x 为偶数时, s g ( x ) = 0 sg(x)=0 sg(x)=0
x x x 为奇数时,若 x x x 为质数,则 s g ( x ) = x 是第几个质数 sg(x)=x是第几个质数 sg(x)=x是第几个质数 ,否则 s g ( x ) = s g ( m i n p x ) sg(x)=sg(minp_x) sg(x)=sg(minpx) m i n p x minp_x minpx x x x 的最小质因子。
s g ( 0 ) = 0 ,   s g ( 1 ) = 1 ,   s g ( 2 ) = 0 ,   s g ( 3 ) = 2 sg(0)=0,\ sg(1)=1,\ sg(2)=0, \ sg(3)=2 sg(0)=0, sg(1)=1, sg(2)=0, sg(3)=2 ,当 x x x 不为质数时,石子数不能变为 m i n p x minp_x minpx 的倍数,并且 x x x 可以变为 ∀ y ∈ [ 1 , m i n p x − 1 ] \forall{y} \in [1, minp_x-1] y[1,minpx1] ,石子数为 m i n p x minp_x minpx 时,也能变为 ∀ y ∈ [ 1 , m i n p x − 1 ] \forall{y} \in [1, minp_x-1] y[1,minpx1],因此 s g ( x ) ≥ s g ( m i n p x ) sg(x)\ge sg(minp_x) sg(x)sg(minpx) 。但 x x x 不能变为 m i n p x minp_x minpx 的倍数的石子数,即所能到达的 s g sg sg 值没有 s g ( m i n p x ) sg(minp_x) sg(minpx) ,因此 s g ( x ) = s g ( m i n p x ) sg(x)=sg(minp_x) sg(x)=sg(minpx)

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 1e7 + 10;

int primes[N], minp[N], sg[N], cnt;
bool vis[N];

void init() {
    int mex = 1;
    sg[1] = 1, sg[0] = 0;
    for(int i = 2; i < N; i ++ ) {
        if(!vis[i]) {
            primes[cnt ++ ] = i, minp[i] = i, sg[i] = mex ++ ;
            if(i == 2) {
                sg[i] = 0;
            }
        }
        for(int j = 0; 1ll * primes[j] * i < N; j ++ ) {
            vis[primes[j] * i] = true;
            if(i % primes[j] == 0) {
                minp[primes[j] * i] = minp[i];
                sg[primes[j] * i] = sg[minp[i]];
                break;
            }
            minp[primes[j] * i] = primes[j];
            sg[primes[j] * i] = sg[primes[j]];
        }
    }

    // for(int i = 0; i <= 100; i ++ ) {
    //     cout << "sg[" << i << "] = " << sg[i] << '\n';
    // }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    init();
    int T;
    cin >> T;
    while(T -- ) {
        int n;
        cin >> n;
        vector<int> a(n);
        for(auto &x : a) cin >> x;

        int ans = 0;
        for(auto x : a) ans ^= sg[x];
        cout << (ans ? "Alice" : "Bob") << '\n';
    }
    return 0;
}
  • 29
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wlzsgl

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

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

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

打赏作者

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

抵扣说明:

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

余额充值