Nim (2) Game Analysis

Source: Internet
Author: User

The game issues come from the beauty of programming.

The question is like this. There are m stones and two players A and B. Player A first splits the stones into several heaps and then follows the Baba ..... the order of the remaining stones continues to take turns to win the players who can take the remaining stones once. Each time you take a rock, each player can only choose one pile of stones from several stacks, and take any number (greater than 0) of the stones. I would like to ask: how does Player A allocate and take stones to ensure that he is sure to win?

Analysis and consideration:We can gradually complicate the problem from the simplest situation, find out the law from it, and then prove the law obtained. This process is a method for analyzing and solving problems.

Set up n stones and M stones

Next we start to analyze from n = 1. At this time, there was only one pile of stones and we took them all at once, so We won first.

N = 2, m = 2, each heap of stones has a stone. Or the first hand wins. M> 2, for (1, x) (x> 1), first hand as long as the X-1 stone in the X heap, then the situation changes ), finally, the first hand wins. For (2, 2), (3, 3), (4, 4)... (M/2, M/2), it is the latter hand to win. Because no matter how many stones are removed from a pile by the first hand, the number of stones in the two piles is unbalanced, and then the second hand can change the number of stones in the two piles to equal (that is, balance ), after the two piles of stones are equal, if (0, 0), the latter wins. If (x, x) (x> 1), proceed with the above method. We can see that when the number of two heaps is equal, the result is always the result of the subsequent operation, and the result is always the result of the first operation when the number is equal, in addition, the first hand cannot keep the two equal stones equal, because only one pile of stones can be changed at a time. The final result is always (0, 0), so the latter can always win. From this we can see that when M is an even number, we only need to divide the stone into two equal piles, and the latter hand will surely win.

In the following analysis, when M is an odd number, for M = 3, 5, 7, the conclusion is that the first hand always wins.

So is this Conclusion Correct? We will prove this conclusion.

The beauty of programming only gives the conclusion. Conclusion:Suppose we break down the stone into N heaps. So there is always XOR (M1, M2,... mn) for an odd number of stones )! = 0 .......... Conclusion 1

Cause: If M is an odd number, it is converted to binary. The last binary bit must be 1, so M1 + M2 + Mn = m, the sum of the last bits of the MI consisting of 0 or 1 is always 1. Then, according to the binary addition: 1 + 1 = In-situ change to 0 and forward to one bits, 0 + 0 = 0 in situ or 0, 1 + 0 = 1 in situ to 1, so we can speculate that M1, M2 ,... mn, the number of M numbers, the last one, is always an odd number. Only the sum of an odd number of 1, and the last digit after the sum can be changed to 1, so that the initial M is an odd number. Since we already know that the last digit in the number of m to be decomposed has an odd number of 1, we do the XOR (the XOR or is similar to the binary summation, but we only need to carry the summation) when the number of M is different or, the last digit must be 1, so we have the above conclusion.

It can be proved; When XOR (M1, M2,... mn )! When the value is 0, we only need to change the value of a certain mi. XOR (M1, m2... MJ... mn) = 0 .............................. conclusion 2

Cause: XOR! = 0 indicates M1, M2 ,... in Mn, there must be some bits. In this m number, the number of 1 of this bits is an odd number (for example, 101,011,111, the total number of 1 on the third bits of each number is 3, while the 1st bits of the 101 number (2nd) are 0, and the 2nd bits of the bits are 1, the total number of the second bits of the three bits is 2, the following analysis is based on this analysis .), We only need to change the number of 1 in some odd 1 bits to an even number of 1 (some bits can be implemented from 0-> 1 or 1-> 0 ). bitwise becomes an even number of 1, since each number can undergo such a change, the key in the second step is to check the number selection. The requirement is to select a variable, and then the number after the change becomes smaller,), so that each bit of the M number has an even number of 1, XOR = 0. This proves the correctness of conclusion 2.

It can be proved that when XOR (M1, M2 ,... when Mn) = 0, we always need to change the value of any mi to make XOR (M1, m2... MJ... mn) = 0 ............... conclusion 3

Cause: When XOR = 0, the total number of 1 (for this m number) on each digit is an even number, so any one of them is changed, it will increase or decrease the number of 1 in some bits. no matter whether the number of 1 is increased by 1 or decreased by 1, it will change the number of 1 in some bits to an odd number, therefore, XOR must be less than 0. this proves the correctness of Conclusion 3.

Conclusion: When M is an odd number, no matter how the heap is divided, the first hand always sets the XOR of these heap stones to 0, the other hand won't set XOR to 0 if it makes any changes without any advice. In the end, only XOR = 0 can win, so it always wins first.By analyzing the binary number of each heap of M stones and the total number of each heap of 1, it is a solution to the above three conclusions.

The above red and bold characters are my proof and understanding of the conclusion in stone Game 2 of the beauty of programming.





Zookeeper

Nim (2) Game Analysis

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.