【ACWing】894. Split-Nim Game

Subject address:

https://www.acwing.com/problem/content/896/

Given nnn piles of stones, the number of stones in each pile isai a_iai, The two players take turns to operate, each operation can take away a pile of stones, and put two piles of stones with a smaller number of stones (you can put 0 0A stone pile with 0 stones, two new piles of stones, there is no limit to the number of stones in any pile, as long as it is less than the taken away), the player who cannot operate is regarded as a failure. Ask whether the first mover has a winning strategy.

Number of data:
1 ≤ n, ai ≤ 100 1 \ le n, a_i \ le 1001n,ai100

First of all, the game will definitely end, which can be proved by mathematical induction. For max ⁡ ai \max a_imaxaiDo induction, if max ⁡ ai = 1 \max a_i=1maxai=1 Then each player can only remove the stones and put them back in two0 00 , it is equivalent to taking away the stones directly, obviously the game will end. Assumingmax ⁡ ai <n \max a_i<nmaxai<The game will end at n , whenmax ⁡ ai = n \max a_i=nmaxai=At n , if the piles of stones are not operated forever, it is equivalent to treating them as non-existent. Under the assumption of induction, the game must be over under the situation composed of other stones. Therefore, the game must go on to a certain moment, and it must be operated. The number ismax ⁡ ai \max a_imaxaiThose piles of stones, then it will lead to max ⁡ ai \max a_imaxaiBecomes smaller, assuming by induction, the game will definitely end (although the number is equal to max ⁡ ai \max a_imaxaiThe pile of rocks is not necessarily unique, but the number will gradually decrease until it becomes 0 00)。

We can regard each pile of stones as an independent situation and find the SG SG corresponding to each pile of stonesS G function value, according to the Sprague-Grundy theorem, find the Nim sum to get theSG SGof the situation composed of piles of stonesS G function value. And theSG SG ofeach pile of stonesS G value isSG SGdepending on the state it can reachS G function value, find the smallest function value that does not appear. The state it can reach is represented by two stone piles. According to the Sprague–Grundy theorem, theSG SGof the situation composed of these two stone pilesS G function value is theirSG SGThe Nim sum of the S G function value. So we only need to enumerate the situations of all two rock piles, calculate the Nim sum of each situation, and then we can find theSG SG ofa pile of rocks.S G function value, and then the total Nim sum is theSG SG ofall the rock piles at the beginningS G function value, so that we can judge whether the first mover has a winning strategy. For ideas, refer tohttps://blog.csdn.net/qq_46105170/article/details/114006814. code show as below:

#include <iostream>
#include <cstring>
#include <unordered_set>
using namespace std;

const int N = 110;

int f[N];

int sg(int x) {
    
    
    if (f[x] != -1) return f[x];

    unordered_set<int> set;
    // 只需要枚举j <= i的情形即可
    for (int i = 0; i < x; i++) 
        for (int j = 0; j <= i; j++)
            set.insert(sg(i) ^ sg(j));

	// 找mex
    for (int i = 0; ; i++) 
        if (!set.count(i)) return f[x] = i;
}

int main() {
    
    
    int n;
    cin >> n;

    memset(f, -1, sizeof f);

    int res = 0;
    for (int i = 0; i < n; i++) {
    
    
        int x;
        cin >> x;
        res ^= sg(x);
    }

    cout << (res != 0 ? "Yes" : "No") << endl;

    return 0;
}

Time complexity O (∑ max ⁡ xi 3) O(\sum \max x_i^3)O ( maxxi3) (In fact, it can't reach so much, because there is memorization, each state only needs to be counted once), spaceO (max ⁡ xi) O(\max x_i)O(maxxi)

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/114011132
Recommended