BZOJ 1022 ||hdu 1907 John (sg函数,sj定理,anti-sg)
题意:n堆石子,每次选一堆,最少拿一个,最多拿光那一堆,拿走最有一个的人输。 问是否有必胜策略。
思路:anti-nim问题。。。
要用到sj定理(是啥。。。?)
参考资料:参考博客
SJ定理
**对于任意一个Anti-SG游戏,如果定义所有子游戏的SG值为0时游戏结束,先手必胜的条件: ** **1、游戏的SG值为0且所有子游戏SG值均不超过1。 ** 2、游戏的SG值不为0且至少一个子游戏SG值超过1。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年07月22日 星期五 23时09分42秒
4File Name :code/hdu/1907.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=5E3+7;
34int n;
35int sg[N];
36bool vis[N];
37
38void sg_init()
39{
40 ms(sg,0);
41
42 for ( int i = 1 ; i < N ; i++)
43 {
44 sg[i] = i;
45 }
46}
47int main()
48{
49 #ifndef ONLINE_JUDGE
50 freopen("code/in.txt","r",stdin);
51 #endif
52
53 sg_init();
54
55 int T;
56 cin>>T;
57 while (T--)
58 {
59 scanf("%d",&n);
60 int sum = 0;
61 int cnt = 0 ;
62 for ( int i = 1 ; i <= n ; i++)
63 {
64 int x;
65 scanf("%d",&x);
66 sum^=sg[x];
67 if (sg[x]>1) cnt++;
68 }
69 if ((sum==0&&cnt==0)||(sum!=0&&cnt>0))
70 {
71 puts("John");
72 }
73 else
74 {
75 puts("Brother");
76 }
77 }
78
79 #ifndef ONLINE_JUDGE
80 fclose(stdin);
81 #endif
82 return 0;
83}