Question: James and little red ginseng Add a new stone-taking game. At the beginning of the game, there were N piles of stones. The two players involved in the game took turns to take away and move the stones. The game started from James. In each round, contestants select a heap with at least one stone and take at least one stone from it. Then, the contestant can move any number of stones from the remaining stones in the heap multiple times to any heap. Of course, the contestant can also not move any stones. However, you must note that contestants must take at least one stone from the selected heap. Therefore,As the game progresses, there are fewer and fewer stones. When it was a player's turn, there was no stone to take away, and the player lost the game. Both Xiao Ming and Xiao Hong are extremely intelligent and can always play games with the best strategy. Given the number of heap N and the number of stones in each heap, can Xiaoming win the game?
In this question, the game is an impartial game. With the application of the Theorem of SD-Grundy, we can calculate some mandatory States, so as to obtain the rule about general mandatory states. However, this article intends to discuss the topic without involving a deep game theory.
Consider n = 1 first. At this time, James can take the pile of stones, so that he can win.
Assume n = 2. If (X, Y) is used to represent the number of two stones in a round (x <= Y), (x, y) can also be regarded as a state in the game. The final state is (0, 0 ). At this time, the contestants were defeated. When it is Xiao Ming's turn, if the current state is (0, x) and X> 0, then the problem is n = 1, so James will win. Consider more complex states, such as (1, x), x> = 1. When x = 1, James will lose. In fact, when the State is (x, x) (the number of the two piles of stones is the same), Xiaoming will be defeated, because no matter how James takes and moves the stones, xiaohong can take appropriate stones and change the status to (Y, Y) type. At the same time, it is easy to see that if it is another type of status, James will win.
N = 3. If James can adjust the state to (0, x, x) by taking away and moving the stones, James can win the final victory. In fact, James does. The proof is as follows: assume that the current status is (x, y, z) without losing its universality. Assume that x <= Y <= z. Then, Y-x <Y <= z. By removing Z-(Y-x)> = 1 stones from the Z heap, and moving the Y-x stones to the X heap, the State (x, y, z) can be changed) convert to (0, Y, Y ).
It seems that the same number of stones is a special situation. However, it is not clear how the situation is for more heaps. Then consider N = 4. Obviously, (,) is a bid, as is. We can guess that James will lose when facing the (x, x, y, Y) state. To prove that this is a defeat, you need to prove that no matter what strategy is adopted, after James finishes this round, the status is not (x, x, y, Y) type, and, in the next round, you can change the status to the (x, x, y, Y) type. In this way, James will eventually be forced to face the final state (0, 0, 0), thus losing the game. The conclusion that the "(x, x, y, Y) state is the mandatory State of James" can be verified by simple thinking.
Now, we need to extend the above conclusions to any number of heaps. When N is an even number, we set the status (x1, x1, x2, X2 ,..., XM, XM), that is, when an even number of heaps exist, and the N heaps can be divided into n/2 groups, each group is composed of two heaps with the same number of stones, it is called the target status. Now we have proved that the target State is James's defeat, while the other State is his victory.
Theorem 1: WHEN n is an even number and the player faces the target status, no matter what strategy the player adopts, the status after the round is over is definitely not the target status.
Proof: Use the reverse verification method. Assume that the proposition is not true. The status after the end of this round can be written as (Y1, Y1, Y2, Y2 ,..., ym, ym), without losing its universality, assuming Y1 <= Y2 <=... <= ym and X1 <= X2 <=... <= XM. Consider the two heaps in the {XK, XK} group. At least one heap is not selected by contestants. Obviously, this heap corresponds to a heap in A group {yt, yt} In (Y1, Y1, Y2, Y2,..., ym, ym. At the same time, because the heap is not selected, the number of stones will not be reduced. Therefore, yt> = XK. Because (x1, x1, x2, X2 ,..., XM, XM) and (Y1, Y1, Y2, Y2 ,..., the heap in ym, ym) has a one-to-one ing relationship. Therefore, each XK corresponds to only one yt with XK <= yt. This shows that 2 (X1 + X2 +... + XM) <= 2 (Y1 + y2 +... + ym), that is, the total number of stones after the wheel is finished is not reduced. But this is impossible. Pass.
Note: In the above proof, we can prove a stronger conclusion: XK <= yk for all K.
Theorem 2: When a contestant is not in the target State, a certain strategy can always be taken to make the state of the target State after the end of the round.
Proof: assume the status is (x1, x2 ,..., XN), X1 <X2 <... <Xn (if there are two heaps with the same number, you do not need to perform any operation on them. Therefore, you can exclude any two heaps with the same number from consideration ). When N = 2 m, select heap XN and perform the following operations: Move the x2k + k stones to the heap x2k, 1 <= k <= S-1. In this way, a total of M = (x3-x2) + (x5-x4) +... + (x2m-1-x2m-2) <xn-x1 from the heap XN needs to be removed. Then, take the xn-M-x1> 0 stones. When N = 2 m + 1 is odd, also select heap XN, and perform the following operation: Move the x2k-x2k-1 stones to the heap x2k-1, 1 <= k <= m. In this way, a total of M = (x2-x1) + (x4-x3) +... needs to be removed from the heap XN... + (x2m-x2m-1) <xn-x1 then, take all the remaining Xn-M> 0 stones.
Theorem: WHEN n is an even number and the initial state is the target State, James will lose. Otherwise, James will win.
At this point, the problem is solved perfectly. The code is very simple. You only need to check the parity of N. When N is an even number, it further checks whether the number of stones in each heap is an even number. If a hash table is used, when n is an even number, the time complexity is O (n); when n is an odd number, O (1 ).
The Code is as follows:
/* N first-hand victory P second-hand victory there is a pile of stones left in this n situation; there are two piles of stones 1, (a, B) a <B; then you can replace it with (A,) form, follow the method to victory, that is, the n situation; 2, (A, A) and the opposite, P situation; three piles of stones (A, B, C) the first hand is always able to convert (M, M, 0) so n; the remaining four piles of stones 1, (a, a, B, B) are similar to the two heap situation, P situation; 2. (A, B, C, D) can always be converted into the (n, n, m, m) form, so the n situation; pushed away in general: if the number of stones in a certain number is odd, it can always be converted to (n, n, m, M ,......) Form, so n situation; if the situation is always (a, a, B, B ,......) Form. If it is the opposite, p is the case. */# Include <iostream> # include <string> # include <cstring> # include <cstdio> # include <algorithm> # define CLR (ARR, Val) memset (ARR, val, sizeof (ARR) using namespace STD; int stone [110]; int main () {// freopen ("input.txt", "r", stdin); int N, i, num, flag; while (scanf ("% d", & N) {CLR (stone, 0); flag = 0; for (I = 0; I <n; ++ I) {scanf ("% d", & num); stone [num] ++; // determine whether the number of stone heaps is paired} for (I = 0; I <110; ++ I) if (stone [I] & 1) Flag = 1; // if not paired, it will win if (FLAG) printf ("Win \ n"); else printf ("lose \ n");} return 0 ;}