题意:
在一个盒子里面有不同颜色的糖果,John和Brother轮流在里面取糖果吃,每次都是John先取。
每次只能取同一种颜色的若干个糖果,不能不取,最后取走糖果的人输。
输出赢家。
思路:
这是一道裸的尼姆博弈,把同一种颜色的看成一堆,就直接套定理的结果就可以了。
如果有不知到尼姆博弈的可以看一下这篇文章
http://www.wutianqi.com/?p=1081
写的不错。
S为利己态,T为利他态。(这个利就是让谁取最后一个)
S0代表超过1的堆数有0个的利己态,S1代表有一个,S2代表有两个或两个以上。T0、T2同理。
必胜态:S2、S1、T0
必输态:T2、S0
Code:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<map>
#include<cctype>
#include<vector>
#define TEST
#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define xep(i, n) for(int i = 0; i < (n); ++i)
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#define dep(i, s, e) for(int i = (s); i >= (e); --i)
#ifdef TEST
#define See(a) cout << #a << " = " << a << endl;
#define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
#define debug(a, s, e){ rep(_i, s, e) {cout << a[_i] << ' '; }cout << endl;}
#define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee);}
#else
#define See(a) {}
#define See2(a, b) {}
#define debug(a, s, e) {}
#define debug2(a, s, e, ss, ee) {}
#endif
const int MAX = 2e9;
const int MIN = -2e9;
const int PI = acos(-1.0);
const double eps = 1e-8;
using namespace std;
const int N = 55;
int a[N];
int nimm(int n)//求状态是T还是S
{
int ret = a[0];
for(int i = 1; i < n; ++i)
{
ret ^= a[i];
}
return ret;
}
int main()
{
int T;
cin >> T;
while(T--)
{
int n;
scanf("%d", &n);
int tem = 0, state;
for(int i = 0; i < n; ++i)
{
scanf("%d", &a[i]);
if(a[i] != 1)
{
++tem;
}
}
state = nimm(n);
bool win;
if(state)
{
if(tem == 0)
{
win = false;
}
else
{
win = true;
}
}
else
{
if(tem == 0)
{
win = true;
}
else
{
win = false;
}
}
printf("%s\n", win ? "John" : "Brother");
}
return 0;
}