Poj2975 Nim game and poj2975nim game
I have been playing the game for a long time since the completion of the provincial competition. I feel like I have forgotten it. Today, I have found a few practitioners who are very interested in this question. In a Nim game, there are several ways to change Nim to 0. (For details about Nim games, refer to: here)
In fact, I think this question is a game coat, and then I will examine your use of the XOR operator (^. Before doing this, we want to understand an important nature of the XOR operator (^:
Now we have three integers a, B, c:
Assume that c = a ^ B, then we can get B = c ^ a, a = B ^ c (the exchange law is true for the XOR or operator ).
Now let's analyze this problem. Suppose sum is Nim sum, a is the number of a pile, and B is the Nim sum of the other stacks. Then we can get sum = a ^ B. Our task is to change sum to zero, so we can achieve our goal as long as we make a equal to B. With the above properties, we can convert the sub-edge to B = sum ^.
Because we cannot change B, we can only start with. In other words, we only need to change a to B. So the question becomes, and the number of a s that can be changed to B. That is to say, there are more than B a (because the question says the stone can only be reduced ).
Now the problem becomes very easy. We can draw a conclusion from this question. The ability to simplify the problem is crucial. In the process of solving the problem, the key is to cultivate this ability. [The Code is as follows]
#include <stdio.h>#define MAXN 1000 + 10int main(){ int n, Nim[MAXN], ans, cnt, i; while(scanf("%d",&n),n){ for(i = ans = 0; i < n; i++){ scanf("%d", &Nim[i]); ans ^= Nim[i]; } for(i = cnt = 0; i < n; i++){ if((ans^Nim[i]) < Nim[i]) cnt++; } printf("%d\n", cnt); } return 0;}