UVA 1566《John》是反 Nim 遊戲:多堆石子,輪流任取一堆任意數量,取走最後一顆石子者輸(標準 Nim 取走最後者贏)。給定初始堆數與大小,判斷先手(John)是否必勝。
xor_sum = a1 ^ a2 ^ ... ^ an,先手勝當且僅當 xor_sum != 0。#include <cstdio>
using namespace std;
const int MAXN = 50;
int main() {
int cas; scanf("%d", &cas);
while (cas--) {
int n, x, xorsum = 0;
bool has_gt1 = false;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &x);
xorsum ^= x;
if (x > 1) has_gt1 = true;
}
if (has_gt1)
puts(xorsum ? "John" : "Brother");
else
puts((n & 1) ? "Brother" : "John");
}
return 0;
}