AC Channel: http://www.lydsy.com/JudgeOnline/problem.php?id=1457
The puzzle, because this is a classic anti-Nim game, then I should come up with a very classical writing very understood article to share.
Topic 1: There are n heap of stones, the first heap has a (i) stones. Two people take in turn, the provisions of each can only from a pile to take a number of roots, can be a bunch of all take away, but not to take, and finally take the end to win, to win the method.
C=a (1) Xor A (2) Xor A (3) XOR, Xor A (n), if c>0, is a self-interested state, denoted by S, if c=0, is denoted as altruistic state, denoted by T.
"Theorem 1" for an S-state, must be able to remove several from a heap of stones, making it a T state.
"Prove" since it is the S state, then at this time c>0, we want to make C into 0.
When C is converted to binary, the highest bit of 1 is the P-bit. Then there must be a 1 of the binary highest bits of a (t) is the P-bit. (Obviously, otherwise C's P-bit cannot be 1)
Then, the number of stones in T heap is changed to X=a (t) XOR C. Because a (t) and C of the binary highest bits of 1 are the same bit. Then the XOR becomes 0. Then x must be less than a (t).
At this point C ' =a (1) Xor A (2) XOR ... xor a (t) XOR C xor A (t+1) xor ... xor a (n). Bring C in, get
C ' =a (1) Xor A (2) XOR ... xor a (n) xor A (1) Xor A (2) XOR ... xor a (n). Obviously C ' = 0.
So, if a (t)-X stone is taken out of a gravel, the S state is changed to T state.
"Theorem 2" for a T state, from any heap to take any of the stones out, will become the S state.
"Proof" is used to disprove the law. The number of stones in the first heap is changed to a (I '). At this time c=0. If C ' >0, then the proposition is set up.
Suppose C ' = 0. Then C ' =a (1) Xor A (2) XOR ... xor a (i ') xor ... xor a (n) = 0.
Because C=0. So C xor C ' = 0. Then a (1) Xor A (2) XOR ... xor a (i) XOR ... xor a (n) xor A (1) Xor A (2) XOR ... xor a (n) = 0.)
A (i) Xor a (i ') = 0. A (i) =a (i) is obviously wrong. So the proposition is to be proven.
Having obtained these two theorems, we can find that any one S state, we can transform it into T state by our own control. What each other does is to turn the T state back to the S state, very passive. So as long as the initiator is the S state, you can always win according to the strategy obtained by Theorem 1. Topic 2: There are n heap of stones, the first heap has a (i) stones. Two people take in turn, the rules can only be taken from a pile of several roots, can be a bunch of all take away, but not to take, and finally take the end of
Negative, the way to win.
Then define a few states. There is only one stone in a heap of stones, which is remembered as a lonely heap. Otherwise be remembered as ample heap.
In the T state, if the number of abundant heaps is greater than or equal to 2, it is recorded as T2 State, 1 ample heaps, recorded as T1 state, there is no ample heap, recorded as T0 state. S0, S1, S2.
"Theorem 3" in S0 state, if the number of lonely heap is odd. That will lose. The T0 state must win.
"Proof" is an odd number of stones, one at a time. It is obvious that those who go first will lose.
"Theorem 4" in the S1 state, the method is correct to win.
"Proof" If the number of lonely heaps is odd, then take out the ample heap, and if it is even, leave the ample heap with only 1 left. This leaves the odd number of lonely heap, the other party to the initiator. By the theorem 3, the other party will lose, and win.
The S2 state of "theorem 5" cannot be changed to T0 state once.
"Proving" it is clear that ample heaps cannot be changed from 2 to 0 at a time.
"Theorem 6" S2 state can be changed to T2 state once.
"Proof" by the theorem 1 s state can be changed to T state once, and will not take the whole heap at once, then the number of abundant heap will not change, by the theorem 5 S2 state cannot be changed to T0 state at once, then the transformation of the T state is T2 state.
The T2 state of "theorem 7" can only be transformed into S1 or S2 states.
"Proof" by the theorem 2, the T-state can only be changed to the S state. Because the abundance of heaps does not change to 0. So it is S1 or S2 state.
"Theorem 8" in the S2 state, as long as the method is correct, will win.
"Proof" is a theorem of 6, which is converted into T2 state first. By theorem 7, the other party can only be converted back to S1 or S2 state. By theorem 4, you win.
"Theorem 9" T2 state must lose.
"Proof" with proof 8.
We got a few winning states: S2,s1,t0. Must lose state: T2,t1,s0.
Compare two questions:
The process of the first question: S2-t2-s2-t2-.....-t2-s1-t0-s0-t0-...-s0-t0 (all 0)
The process of the second question: S2-t2-s2-t2-.....-t2-s1-s0-t0-s0-...-s0-t0 (all 0)
We can see that the previous process is the same. The key is to get the S1 state, how to choose to make themselves win. And this is what you can master.
Therefore, we only need to leave the T2 state to each other, sooner or later he will be transformed into a S1 state. You will win.
Then make a few more patches:
1. The T1 state is not, because if there is only a sufficient heap, then the different or up can not be equal to 0.
2. Amendments to theorem 6:
Not that the number of heaps is constant. There is a possibility of change.
But can not become T0 at once, and because no T1 exists, so it is transferred to T2 state, but the heap number may change:
[Example]:
1 2 2 4-5
Operation once 1 2 2 1 0 so theorem 6 is still correct, but there are some non-rigorous statements.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intMain () {#ifndef Online_judge freopen ("1022.in","R", stdin); Freopen ("1022.out","W", stdout);#endif intkase,n,x; scanf ("%d",&Kase); while(kase--){ intans=0, cnt=0; scanf ("%d",&N); for(intI=1; i<=n;i++) scanf ("%d", &x), ans^=x,cnt+= (x>1); if(!ans) { if(!cnt) puts ("John"); ElsePuts"Brother"); } Else{ if(!cnt) puts ("Brother"); ElsePuts"John"); } } return 0;}
View Code
Bzoj 1022 [SHOI2008] Little John's game John