Nim or not Nim?
Problem descriptionnim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. on each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.
Nim is usually played as a misere game, in which the player to take the last object loses. nim can also be played as a normal play game, which means that the person who makes the last move (I. E ., who takes the last object) wins. this is called normal play because most games follow this Convention, even though Nim usually does not.
Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. that is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.
Inputinput contains multiple test cases. the first line is an integer 1 ≤ T ≤ 100, the number of test cases. each case begins with an integer N, indicating the number of the heaps, the next line contains N integers s [0], s [1],..., s [N-1], representing heaps with s [0], s [1],..., s [N-1] objects respectively. (1 ≤ n ≤ 10 ^ 6, 1 ≤ S [I] ≤ 2 ^ 31-1)
Outputfor each test case, output a line which contains either "Alice" or "Bob", which is the winner of this game. Alice will play first. You may asume they never make mistakes.
Sample Input
232 2 323 3
Sample output
AliceBob
Source2009 multi-university training contest 13-host by hit
Question:
Alice and Bob take n stones in turn, each heap s [I], Alice first, each time can take any stone from any pile, you can also divide a pile of stones into two small heaps. The winner wins. (1 ≤ n ≤ 10 ^ 6, 1 ≤ S [I] ≤ 2 ^ 31-1)
Solution:
For a given directed acyclic graph, the following G function is defined for each vertex of the graph: g (x) = Mex {G (y) | Y is the successor of x}. Here g (x) is SG [x]
For example, if there are 1 pile of N stones, only {1, 3, 4} stones can be taken at a time. What is the SG value of each number when the stone winner is obtained first?
SG [0] = 0,
When n = 1, {1} stones can be taken away, with {0} remaining stones and Mex {SG [0] }={ 0} remaining. Therefore, SG [1] = 1;
When n = 2, {1} stones can be taken away, and {1} stones are left. Therefore, SG [2] = 0;
When n = 3, {1, 3} stones can be taken away, with {2, 0} remaining stones, Mex {SG [2], SG [0] }={ 0, 0 }, therefore, SG [3] = 1;
When N = 4, {1, 3, 4} stones can be taken away, and {3, 1, 0} stones are left, Mex {SG [3], SG [1], SG [0] }={ 1, 1, 0}, so SG [4] = 2;
When n = 5, {1, 3, 4} stones can be taken away, with {, 1} remaining stones, Mex {SG [4], SG [2], SG [1] }={ 2, 0, 1}, so SG [5] = 3;
And so on .....
X 0 1 2 3 4 5 6 7 8 ....
SG [x] 0 1 0 1 2 3 2 0 1 ....
Therefore, for this question:
SG [0] = 0
SG [1] = Mex {SG [0]} = 1
SG [2] = Mex {SG [0], SG [1], SG []} = Mex {, 1 ^ 1} = 2;
SG [3] = Mex {SG [0], SG [1], SG [2], SG [1, 2]} = Mex {0, 1, 2, 1 ^ 2} = Mex {0, 1, 2, 3} = 4;
SG [4] = Mex {SG [0], SG [1], SG [2], SG [3], SG [1, 3], SG []} = Mex {,} = 3;
........................................ ......................................
We can find that SG [4 * k + 1] = 4 * k + 1, SG [4 * k + 2] = 4 * k + 2, SG [4 * k + 3] = 4 * k + 4, SG [4 * k + 4] = 4 * k + 3
Solution code:
#include <iostream>using namespace std;int main(){ int t; cin>>t; while(t-- >0){ int sg=0,n,x; cin>>n; for(int i=0;i<n;i++){ cin>>x; if(x%4==0) sg^=x-1; else if(x%4==3) sg^=x+1; else sg^=x; } if(sg) cout<<"Alice"<<endl; else cout<<"Bob"<<endl; } return 0;}