Nim game
Q: I have m-heap cards. Two people win any (not less than one) cards in a stack, and the final winner is the winner. How many methods are there to win the first card.
Train of Thought: 1) if we give a bid: a1 ^ a2 ^... ^ an = 0, the first hand won't win any;
2) If the winning status is given: a1 ^ a2 ^ ....... ^ an = k (where k is not zero), our goal is to win
Turn to the "fail" status and make the first hand win. If a1 ^ a2 ^... ^! = 0, there must be a legitimate move, the ai
After changing to ai, a1 ^ a2 ^... ^ ai '^... ^ an = 0. If a1 ^ a2 ^... ^ an = k, there must be an ai,
Its binary value indicates that the maximum bit of k is 1 (otherwise, how does one obtain the highest bit of k ). At this time, ai ^ k <ai must
Yes. Then we can change ai to ai '= ai ^ k. At this time, a1 ^ a2 ^... ^ ai '^... ^ an = a1 ^ a2 ^... ^ an ^ k = 0.
[Cpp]
# Include <iostream>
# Include <cstdio>
Using namespace std;
Int main ()
{
Int a [102], m, I, sum, s, count;
While (scanf ("% d", & m )! = EOF & m)
{
Sum = count = 0;
For (I = 0; I <m; I ++)
{
Scanf ("% d", & a [I]);
Sum = sum ^ a [I];
}
For (I = 0; I <m; I ++)
{
S = sum ^ a [I];
If (s <a [I])
Count ++;
}
Printf ("% d \ n", count );
}
Return 0;
}