Game - Explanation of the Game of Nimes

        Motif: There are several piles of stones, and the number of each pile is limited. Two people take random stones from these piles in turn, at least one (must not be excluded), and the last person who takes all the stones wins.

1. Let's take a pile as an example: Suppose there is only a pile of stones, your best option is to take all the stones, then you will win.

2. If it is two piles: Assuming that there are two piles of stones and the number is different, then your best choice is to take the extra stones from the pile of stones, so that the number of stones in the two piles is the same. How to take the other, you can take the same number with him in the other pile, and you will win in this situation. For example, there are two piles of stones, the first pile has 3 stones, and the second pile has 5 stones. At this time, you have to take three stones from the second pile, and then both piles become 3 stones. How to operate, you can "learn" him, for example, if he takes two in the first pile, you can take two in the second pile, so you are sure to win

3. If it is three piles, we use (a, b, c) to represent a certain situation. First (0, 0, 0) is obviously a strange situation. No matter who faces the strange situation, it will inevitably fail. The second singular situation is (0, n, n), as long as the opponent takes as many items, it will eventually lead to (0, 0, 0). After careful
analysis , (1, 2, 3) is also a strange situation. No matter how the opponent takes it, it can become a situation of (0, n, n).

From this we have to understand two theories:

A state is a win-win state if and only if all its successors are win-win states A state is a win-win state if and only if it has at least one successor a win-win state 

        With these two rules, you can use the recursive method to determine whether each node of the entire state diagram is a must-win or must-defeat state. 

Here we introduce the theorem given by L. Bouton in 1902: the state (x1, x2, x3) is a must-fail state if and only if x1 XOR x2 XOR x3=0, where XOR is a binary bit-by-bit XOR operation, also into Nim and .

That is when Nim and! = 0, first mover wins, otherwise loses 

In computer algorithms, there is an operation called bitwise modulo 2 addition, also known as XOR. We use the symbol (+) to represent this operation. The difference between this operation and general addition is that 1+1=0. First look at the result of the bitwise modulo 2 addition of (1, 2, 3):

1 = binary 01
2 = binary 10
3 = binary 11 (+)
—————
0 = binary 00 (note no carry)

    The same is true for the singular situation (0, n, n), and the result is also 0.

    Any singular situation (a, b, c) has a(+)b(+)c = 0.

If we are faced with a non-singular situation (a, b, c), how do we turn it into a singular situation? Assuming a < b < c, we only need to change c to a (+) b, because there are the following operation results: a (+) b (+) (a (+) b) = (a (+) a)(+)(b(+)b)=0(+)0=0. To turn c into a(+)b, just subtract c-(a(+)b) from c. That is, take (a(+)b) stones. Here we want to understand a feature of XOR operation a(+)c(+)c = aExample

    1. (14, 21, 39), 14(+) 21=27, 39-27=12, so take 12 objects from 39 to reach the singular situation (14, 21, 27).

    Example 2. (55, 81, 121), 55(+) 81=102, 121-102=19, so taking 19 items from 121 forms a strange situation (55, 81, 102).

    Example 3. (29, 45, 58), 29(+) 45=48, 58-48=10, take 10 from 58 and become (29, 45, 48).

    Example 4. Let's actually play a game to see:
        A: (7,8,9)->(1,8,9) Strange situation
        B: (1,8,9)->(1,8,4)
        A: (1,8,4)->(1,5,4) Strange situation
        B: (1,5,4)->(1,4,4)
        A: (1,4,4)->(0, 4,4) Singular situation
        B: (0,4,4)->(0,4,2)
        A:(0.4,2)->(0,2,2)Singular situation
        B:(0,2,2 )->(0,2,1)
        A:(0,2,1)->(0,1,1)Singular situation
        B:(0,1,1)->(0,1,0)
        A: (0,1,0)->(0,0,0) singular situation

        A wins.

The first type: it is the person who is allowed to judge the victory, the difference or sum of n piles of stones, according to when Nim sum! = 0, the first mover wins, otherwise the failure can be judged.

The second type: the one who finishes first is judged to lose, count all the numbers greater than 1, and XOR all the numbers once, if the number greater than 1 is0&& the XOR sum is 0|| If the number is greater than 0 && XOR sum is not zero, the first hand wins, otherwise the second hand wins.

The third type: limit the maximum number of stones to be taken, for example, there are m stones in the i-th pile, and at most r stones are taken. First, m=m% (r+1); then XOR is performed. Then judge the winner or lose according to the XOR sum.

Type 4: The first mover wants to win, and how many options are there for the first move. When the first mover must lose, it is obviously 0. If the first mover wins, then the first mover must work hard to create a strange situation, that is, to make the XOR and sum of the remaining stones equal to 0. The above has explained how to convert a non-singular situation into a strange situation. When a certain position of the nim game: (x1,x2,x3), if and only if the nim of its parts - sum = 0 (ie x1 (+) x2 (+) x3 = 0 (ie the XOR of the parts is 0)) The current position is a must-fail point, which is also applicable to the case of multiple heaps. We first find the sum of the XORed values ​​of all the heaps, and then use this value to XOR each heap, let res = x1 (+) sum (sum is the XORed sum of all the heaps). If res < x1, the current player will take (x1-res) from x1, and multiplying x1 by res will inevitably lead to the XOR value of all heaps being 0, which is the point of inevitable defeat (reaching a singular situation), This is a plan. Traverse each heap and make the above judgments to get the total number of solutions.

res = x1(+)sum; in fact, it is the n-1 heap XOR sum except x1, a(+)b(+)c=sum; sum(+)c=a(+)b(+)c (+)c=a(+)b;

ps: Note that one must-fail point cannot lead to another must-fail point, because if this is the case, the current must-fail point is not a must-fail point, so there is at most one method for each heap operation.

It can lead to a must-defeat point. If res > x1, no matter how much is taken from this heap, it cannot lead to a must-defeat point! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939456&siteId=291194637