John
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 4479 Accepted Submission(s): 2567
Problem Description
Little John is playing very funny game with his younger brother. There is one big box filled with M&Ms of different colors. At first John has to eat several M&Ms of the same color. Then his opponent has to make a turn. And so on. Please note that each player has to eat at least one M&M during his turn. If John (or his brother) will eat the last M&M from the box he will be considered as a looser and he will have to buy a new candy box.
Both of players are using optimal game strategy. John starts first always. You will be given information about M&Ms and your task is to determine a winner of such a beautiful game.
Both of players are using optimal game strategy. John starts first always. You will be given information about M&Ms and your task is to determine a winner of such a beautiful game.
Input
The first line of input will contain a single integer T – the number of test cases. Next T pairs of lines will describe tests in a following format. The first line of each test will contain an integer N – the amount of different M&M colors in a box. Next line will contain N integers Ai, separated by spaces – amount of M&Ms of i-th color.
Constraints:
1 <= T <= 474,
1 <= N <= 47,
1 <= Ai <= 4747
Constraints:
1 <= T <= 474,
1 <= N <= 47,
1 <= Ai <= 4747
Output
Output T lines each of them containing information about game winner. Print “John” if John will win the game or “Brother” in other case.
Sample Input
2
3
3 5 1
1
1
Sample Output
John
Brother
Source
题意:n堆石子 两个人轮流拿某一堆的石子 最少拿一个 拿走最后一个的输
题解:nim博弈的分析 重在分析 贴Fsss的分析 链接
经典的Nim博弈的一点变形。设糖果数为1的叫孤独堆,糖果数大于1的叫充裕堆,设状态S0:a1^a2^..an!=0&&充裕堆=0,则先手必败(奇数个为1的堆,先手必败)。S1:充裕堆=1,则先手必胜(若剩下的n-1个孤独堆个数为奇数个,那么将那个充裕堆全部拿掉,否则将那个充裕堆拿得只剩一个,这样的话先手必胜)。T0:a1^a2^..an=0&&充裕堆=0,先手必胜(只有偶数个孤独堆,先手必胜)。S2:a1^a2^..an!=0&&充裕堆>=2。T2:a1^a2^..an=0&&充裕堆>=2。这样的话我们用S0,S1,S2,T0,T2将所有状态全部表示出来了,并且S0先手必败,S1、T0先手必胜,那么我们只需要对S2和T2的状态进行分析就行了。(a)S2可以取一次变为T2。(b)T2取一次可变为S2或者S1。因为S1是先手必胜态,那么根据a,b这两个转换规则,我们就能得知S2也是先手必胜,T2是先手必败。
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 #include<bits/stdc++.h> 8 #include<map> 9 #include<set> 10 #include<cmath> 11 #include<queue> 12 #include<bitset> 13 #include<math.h> 14 #include<vector> 15 #include<string> 16 #include<stdio.h> 17 #include<cstring> 18 #include<iostream> 19 #include<algorithm> 20 #pragma comment(linker, "/STACK:102400000,102400000") 21 using namespace std; 22 #define A first 23 #define B second 24 const int mod=1000000007; 25 const int MOD1=1000000007; 26 const int MOD2=1000000009; 27 const double EPS=0.00000001; 28 typedef __int64 ll; 29 const ll MOD=1000000007; 30 const int INF=1000000010; 31 const ll MAX=1ll<<55; 32 const double eps=1e-14; 33 const double inf=~0u>>1; 34 const double pi=acos(-1.0); 35 typedef double db; 36 typedef unsigned int uint; 37 typedef unsigned long long ull; 38 int t; 39 int n; 40 int ans,cou; 41 int main() 42 { 43 while(scanf("%d",&t)!=EOF) 44 { 45 for(int i=1;i<=t;i++) 46 { 47 scanf("%d",&n); 48 ans=cou=0; 49 int exm; 50 for(int j=1;j<=n;j++) 51 { 52 scanf("%d",&exm); 53 ans^=exm; 54 if(exm>1) 55 cou++; 56 } 57 if((ans==0&&cou>=2)||(cou==0&&ans%2!=0)) 58 printf("Brother "); 59 else 60 printf("John "); 61 } 62 } 63 64 return 0; 65 }