Poj2975 Nim game and poj2975nim game

Source: Internet
Author: User

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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.