题解 P4279 【[SHOI2008]小约翰的游戏】

· · 题解

题意

反Nim游戏,两人轮流选一堆石子拿,拿到最后一个的输.问先手是否必胜.

分析

怎么说,分类讨论?

所以这道题首先特判一下情形1,然后只用异或起来看等不等于0就行了.

(我在想Anti-SG是不是可以看作两个人都希望对方赢而斗智斗勇的普通SG...)

CODE

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
template<typename T>void read(T &num) {
    char ch; int flg=1;
    while((ch=getchar())<'0'||ch>'9')if(ch=='-')flg=-flg;
    for(num=0;ch>='0'&&ch<='9';num=num*10+ch-'0',ch=getchar());
    num*=flg;
}
int n, x;
int main() {
    int T; read(T);
    while(T--) {
        read(n);
        int ans = 0, sum = 0;
        for(int i = 1; i <= n; ++i) {
            read(x), ans ^= x;
            sum += (x==1);
        }
        if(sum == n) puts((n&1) ? "Brother" : "John");
        else puts(ans ? "John" : "Brother");
    }
}