[BZOJ 1188] [HNOI 2007] 分裂游戏

Description

https://www.lydsy.com/JudgeOnline/problem.php?id=1188

Solution

每个豆子是一个单独的游戏,\(SG\) 函数的下标是豆子的位置。

如果一个位置上有偶数个豆子,胜负结果是不会改变的,因为后手可以一直模仿先手的动作。因此所有位置的豆子等价于这个位置的豆子总数对 \(2\) 取模的余数。

Code

#include <cstdio>
#include <cstring>

int T, n, ans, cnt, a[50], sg[50], vis[50];

void getsg() {
    for (int i = n - 1; i; --i) {
        for (int j = i + 1; j <= n; ++j)
            for (int k = j; k <= n; ++k)
                vis[sg[j]^sg[k]] = i;
        for (int j = 0; ; ++j)
            if (vis[j] != i) { sg[i] = j; break; }
    }
}

int main() {
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n), ans = cnt = 0;
        memset(sg, 0, sizeof sg);
        memset(vis, 0, sizeof vis);
        for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
        getsg();
        for (int i = 1; i <= n; ++i)
            if (a[i] & 1) ans ^= sg[i];
        if (ans) {
            for (int i = 1; i <= n; ++i)
                if (a[i]) for (int j = i + 1; j <= n; ++j)
                    for (int k = j; k <= n; ++k)
                        if (!(ans ^ sg[i] ^ sg[j] ^ sg[k]))
                            if (++cnt == 1) printf("%d %d %d\n", i - 1, j - 1, k - 1);
            printf("%d\n", cnt);
        } else puts("-1 -1 -1"), puts("0");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fly-in-milkyway/p/10010890.html