-
Notifications
You must be signed in to change notification settings - Fork 11
/
vjudge_UVA-1566_John.cpp
64 lines (58 loc) · 1.47 KB
/
vjudge_UVA-1566_John.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**vjudge - UVA-1566
* Classic Misere Nim game, follow the theory and it'll be easy.
*
* Time: O(n), Space: O(n)
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
int getNimSum(vector<int> &piles) {
int nim = 0;
for (int i = 0; i < (int)piles.size(); i++) {
nim ^= piles[i];
}
return nim;
}
bool WinNimGame(vector<int> &piles) { // first player wins or not?
int nim = getNimSum(piles);
if (nim != 0) {
return true;
}
return false;
}
bool WinMisereNimGame(vector<int> &piles) {
int n_mt1 = 0, n_one = 0;
for (int i = 0; i < (int)piles.size(); i++) {
if (piles[i] == 1)
n_one++;
else
n_mt1++;
}
if (n_mt1 >= 1) {
// force opponent to move to 1 pile with >1 stones, then adjust that to 0 or 1
return WinNimGame(piles);
} else {
return (n_one % 2 == 0);
}
}
int n;
vector<int> piles;
int main() {
int num_tc;
cin >> num_tc;
while (num_tc--) {
cin >> n;
piles.resize(n);
for (int i = 0; i < n; i++) cin >> piles[i];
cout << (WinMisereNimGame(piles) ? "John\n" : "Brother\n");
}
return 0;
}