洛谷 P4279 [SHOI2008]小约翰的游戏
题目分析
可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取,我们规定取到最后一粒石子的人算输。
根据题目中给出的上述信息,我们可以知道这是一个 Anti-SG 游戏
那么我们就通过SJ定理来解决。
SJ 定理:对于一个 Anti−SG 游戏,如果我们规定当前局面中所有单一游戏的 SG 为 0 时,游戏结束,则先手必胜的条件为:
- 游戏的SG值不为0, 且存在一个单一游戏的SG值大于1。
- 游戏的SG为0, 且不存在任意一个单一游戏的SG值大于1。
根据 SJ 定理解决即可。
code
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define N 100010
#define M 1010
using namespace std;
int T, n, a;
int read() {
int s = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
int main() {
T = read();
while (T--) {
n = read(); bool yi = 1;
int x = 0;
for (int i = 1; i <= n; i++) {
a = read();
if (a ^ 1) yi = 0;
x ^= a;
}
int flag;
if (yi) flag = (x == 0);
else flag = x;
if (flag) puts("John");
else puts("Brother");
}
}