ac number theory of Nimes game

Nimm's Game
question


type The Nimm game model is roughly like this:


there are 3 piles of several items, two people take turns to take as many items from a pile, and it is stipulated that at least 1 item is taken at a time, and more There is no limit to whoever, and the one who takes the light in the end wins.


 
Analysis
1. First of all, think about it yourself, you will find that as long as the last two piles have the same amount of items (not zero) and the third pile is zero, then the side facing this situation will lose
. Then we use (a, b, c) Indicates a certain situation. First of all, (0,0,0) is obviously a must-fail state. No matter who faces (0,0,0), it will inevitably fail; the second must-fail state is (0,n,n ), you take k (k ≤ n) items from a certain pile, no matter how much k is, as long as the opponent takes k items from another pile, he will finally face the situation of (0,0,0), must lose. After careful analysis, (1, 2, 3) is also a must-lose state. No matter how you take it, the opponent can change the situation to (0, n, n)
. What are the characteristics of this strange situation?
I don't know who is so arrogant to be able to associate this situation with binary.
Here is an operation symbol, XOR '^', a^b=a'b+ab' (a' is not a),
 
we use The symbol XOR represents this operation, which differs from general addition in that 1 XOR 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 XOR
———————
0 = binary 00 (note no carry)
 
For singular situations The same is true for (0,n,n), and the result is also 0.
Any singular situation (a,b,c) has a XOR b XOR c = 0
 
If we are faced with a non-defeating state (a, b, c), how do we become a must-defying state?
Assuming a < b < c, we just need to change c to a XOR b. Because there is the following operation result:
a XOR b XOR (a XOR b)=(a XOR a) XOR (b XOR b) = 0 XOR 0 = 0
To change c to a XOR b, just do c-( a XOR b) This kind of operation can be
 
2. The Nim game model can be extended to: there are n piles of several items, two people take turns to take any number of items from a certain pile, and it is stipulated that at least one is taken at a time, and there is no limit to the number of items. , and the one who takes the light in the end wins.
The variables in this game are the number of piles k and the number of items in each pile N1, N2, ..., Nk.
The corresponding combinatorial problem is to determine whether the first player wins or the second player wins and how the two players should take items to ensure that they win
 
.
If the game starts with a pile of items, the first player wins by taking all the items. There are now 2 piles of items, and the number of items are N1 and N2. The player's victory does not depend on the specific value of N1 and N2, but on whether they are equal.
That is to say, we have the strategy of two heaps. Now how do we extend the sub-take strategy of two heaps to any number of heaps?
 
First recall that every positive integer has a corresponding binary number, for example: 57(10) = 111001(2), that is: 57(10)=25+24+23+20. Thus, we can think of each pile of items as consisting of sub-heaps that are powers of 2. In this way, the large heap containing 57 items can be regarded as composed of sub-heaps with numbers of 25, 24, 23, and 20.
 
Now consider a general Nim game with heap sizes N1, N2, ... Nk. Represent each number Ni as its binary number (the number of digits is equal, and 0 is added in front if it is not equal):
N1 = as…a1a0
N2 = bs…b1b0
……
Nk = ms…m1m0
If the number of sub-heaps of each size is an even number, we say that the Nim game is balanced, and the addition of the corresponding bits is an even number. It is a balanced bit, otherwise it is called an unbalanced bit. Therefore, a Nim game is balanced if and only if:
as +bs + … + ms is even, i.e. as XOR bs XOR … XOR ms = 0

a1 +b1 + … + m1 is even, i.e. a1 XOR b1 XOR … XOR m1 = 0
a0 +b0 + … + m0 is an even number, i.e. a0 XOR b0 XOR … XOR m0 = 0
  
So, we can get the first-hand winning strategy in the game of Nim:
Bouton’s theorem: The first-hand can be in a non-equilibrium Nim win the game, and the second hand can win the balanced Nimes game. That is, the state (x1, x2, x3, …, xn) is the P state if and only if x1 xor x2 xor x3 xor … xor xn =0. Such an operation is also called Nim Sum.
We experiment with a Nim game with two stacks of items. The game is assumed to be in an unbalanced state at the start of the game. In this way, the first player can use a way to take the stones, so that after he takes the stones, what he leaves to the second player is a game in a balanced state, and then no matter how the second player takes the stones, the game left to the first player must be an unbalanced game. This is repeated, and when the second hand takes the pieces in the last balanced state, the first hand can take all the items at one time and win. And if the game is in a balanced state at the beginning of the game, then take the child according to the above method, and finally the second hand can get
 
the following application of this winning strategy to consider the 4-pile Nim game. The piles are 7, 9, 12, and 15 coins in size. In binary, the numbers are: 0111, 1001, 1100 and 1111
Therefore, the following table can be obtained:




 From the equilibrium conditions of the Nim game, it can be known that this game is a Nim game in an unbalanced state. Therefore, the first mover must be able to achieve the final victory according to the winning strategy. There are many specific methods. The first player can take 11 coins from the pile of size 12 to make the game balance (as shown in the table below)






. No matter how the second player takes the coins, the first player can still make the game balance after taking the coins. The


same In principle, the first mover can also choose a pile of size 9 and take 5 coins and leave 4 coins, or, the first mover can take 13 coins from the pile of size 15 and leave 2 coins At the
end of the day, the game of Nim is all about the game What state (balanced or unbalanced) the game is in at the beginning and whether the first player can play the game according to the winning strategy of the subgame
When the number of piles is greater than 2, we see that Bouton's theorem still applies, and the following is proved by mathematical induction
  
: If every heap is 0, it is obviously P state (must lose). The last two recursive relationships between the P state and the N state are verified below:
1. Each N state can reach the P state in one step.
The proof is constructive. Check the leftmost 1 in the binary representation of Nim and X, then randomly pick an item pile Y whose bit is 1, and adjust it according to the Nim sum (0 becomes 1, 1 becomes 0). For example, the Nim sum is 100101011, and one of them is 101110001. In order to make the Nim sum become 0, it is only necessary to take the XOR of the number of items before the operation and Nim.
Obviously , the number of items after the operation becomes smaller, so the sum is legal. Let the Nim sum of other heaps before the operation be Z, then there is Y xor Z = X. The Nim sum after the operation is X xor Y xor Z = X xor X = 0, which is a P state.
2. Each P state (must win state) cannot reach the P state in one step.
Because only a bunch of items can be changed, no matter Which bit of it is modified, the corresponding bit of Nim must not be 0, and it cannot be the P state.
This proves Bouton's theorem
 
In the actual solution of the
Nim game, if it is stipulated that the last player to take the light loses, what is the situation?
At first glance, the problem is a lot more complicated (because you can't take the initiative, you have to "hide" to take it, so as not to get the last item), but for Nim games, it is almost the same:
first follow the same strategy as the normal rules , until there is exactly one heap x with a number of items greater than 1. In such a case, it is only necessary to take the items in pile x until there is only 1 item left or take them all, so that the opponent is faced with an odd pile of items, each of which has exactly 1 item. Such a situation is obviously inevitable. Since you need to ensure that the Nim sum is 0 after each operation, it is impossible to "exactly have a heap with an item count greater than 1" for the first time after your operation. The new game has been perfectly solvedBeing
 
a Good Boy in Spring FestivalProblem
 


DescriptionParents are
always concerned after a year away from home. Can you be a good child for a few days when you come home during the Spring Festival? Try to do the following things during winter vacation. A small gift, take the initiative and strongly request to wash the dishes once, get up early one day and cook breakfast for your parents carefully. If you want, you can also tell your parents, let’s play a small game, let’s play a small game in ACM class. : There are M piles of playing cards on the table; the number of each pile is Ni (i=1...M); two people take turns; each step can choose a pile and take any card from it; When all the cards are drawn, the game ends; the person who draws the cards last is the winner. Now we don't want to study whether the first mover wins or loses. I just want to ask you: ——"If the first mover wants to win, what options are there for the first move?" Input

















 

The input data contains multiple test cases, and each test case occupies 2 lines. The first line contains an integer M (1<M<=100), which represents the number of stacks of playing cards, and the next line contains M integers Ni (1<= Ni<=1000000, i=1...M), respectively representing the number of M piles of poker. M is 0 to indicate the end of the input data.
 
Output
If the first player can win, please output the number of feasible solutions for his first step, otherwise, please output 0, and the output of each instance occupies one line.
 
Sample Input
3
5 7 9
0
 
Sample Output

1


# include<stdio.h>
intmain()
{
    int nPile,i,sum,cnt;
    int p[105];
    while(scanf("%d",&nPile)&&nPile)
    {
        cnt=0;
        sum=0;
        for(i=0;i<nPile;i++)
        {
            scanf("%d",&p[i]);
            sum^= p[i];
        }
        for(i=0;i<nPile;i++)
        {
            if(p[i]>(sum^p[i]))//Note that the priority of '^' is less than '>'
                cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

John

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 5612    Accepted Submission(s): 3247


Problem Description
Little John is playing very funny game with his younger brother. There is one big box filled with M&Ms of different colors. At first John has to eat several M&Ms of the same color. Then his opponent has to make a turn. And so on. Please note that each player has to eat at least one M&M during his turn. If John (or his brother) will eat the last M&M from the box he will be considered as a looser and he will have to buy a new candy box.

Both of players are using optimal game strategy. John starts first always. You will be given information about M&Ms and your task is to determine a winner of such a beautiful game.

 

Input
The first line of input will contain a single integer T – the number of test cases. Next T pairs of lines will describe tests in a following format. The first line of each test will contain an integer N – the amount of different M&M colors in a box. Next line will contain N integers Ai, separated by spaces – amount of M&Ms of i-th color.

Constraints:
1 <= T <= 474,
1 <= N <= 47,
1 <= Ai <= 4747

 

Output
Output T lines each of them containing information about game winner. Print “John” if John will win the game or “Brother” in other case.

 

Sample Input
 
  
2 3 3 5 1 1 1
 

Sample Output
 
  
John Brother

The meaning of the question: There are n boxes containing several candies. Two people take turns to take candies. They can only take candies in one box. At least one candy can be taken out of the whole box at most. The one who finishes the last time loses .

分析:经典的Nim博弈的一点变形。设糖果数为1的叫孤独堆,糖果数大于1的叫充裕堆,设状态S0:a1^a2^..an!=0&&充裕堆=0,则先手必败(奇数个为1的堆,先手必败)。S1:充裕堆=1,则先手必胜(若剩下的n-1个孤独堆个数为奇数个,那么将那个充裕堆全部拿掉,否则将那个充裕堆拿得只剩一个,这样的话先手必胜)。T0:a1^a2^..an=0&&充裕堆=0,先手必胜(只有偶数个孤独堆,先手必胜)。S2:a1^a2^..an!=0&&充裕堆>=2。T2:a1^a2^..an=0&&充裕堆>=2。这样的话我们用S0,S1,S2,T0,T2将所有状态全部表示出来了,并且S0先手必败,S1、T0先手必胜,那么我们只需要对S2和T2的状态进行分析就行了。(a)S2可以取一次变为T2。(b)T2取一次可变为S2或者S1。因为S1是先手必胜态,那么根据a,b这两个转换规则,我们就能得知S2也是先手必胜,T2是先手必败。


#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=100010;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
typedef double db;
typedef unsigned long long ull;
int main()
{
    int a,i,n,t,g,xo;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        g=xo=0;
        for (i=1;i<=n;i++) {
            scanf("%d", &a);
            xo^=a;if (a>1) g++;
        }
        if ((g==0&&n&1)||(g>1&&!xo)) printf("Brother\n");
        else printf("John\n");
    }
    return 0;
}

Guess you like

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