BZOJ 1022 ||hdu 1907 John (sg函数,sj定理,anti-sg)

hdu1907题目链接

题意:n堆石子,每次选一堆,最少拿一个,最多拿光那一堆,拿走最有一个的人输。  问是否有必胜策略。

思路:anti-nim问题。。。

要用到sj定理(是啥。。。?)

参考资料:参考博客

SJ定理

**对于任意一个Anti-SG游戏,如果定义所有子游戏的SG值为0时游戏结束,先手必胜的条件: ** **1、游戏的SG值为0且所有子游戏SG值均不超过1。 ** 2、游戏的SG值不为0且至少一个子游戏SG值超过1。

/* ***********************************************
Author :111qqz
Created Time :2016年07月22日 星期五 23时09分42秒
File Name :code/hdu/1907.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=5E3+7;
7int n;
8int sg[N];
9bool vis[N];
1void sg_init()
2{
3    ms(sg,0);
 1    for ( int i = 1 ; i < N ; i++)
 2    {
 3	sg[i] =  i;
 4    }
 5}
 6int main()
 7{
 8	#ifndef  ONLINE_JUDGE 
 9	freopen("code/in.txt","r",stdin);
10  #endif
	sg_init();
 1	int T;
 2	cin>>T;
 3	while (T--)
 4	{
 5	    scanf("%d",&n);
 6	    int sum = 0;
 7	    int cnt = 0 ;
 8	    for ( int i = 1 ; i <= n ; i++)
 9	    {
10		int x;
11		scanf("%d",&x);
12		sum^=sg[x];
13		if (sg[x]>1) cnt++;
14	    }
15	    if ((sum==0&&cnt==0)||(sum!=0&&cnt>0))
16	    {
17		puts("John");
18	    }
19	    else
20	    {
21		puts("Brother");
22	    }
23	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}