Before introducing the SG function and the SG theorem, let's introduce the winning and losing points.
the concept of winning points and losing points:P-Point: Must lose the point, in other words, is who is in this position, then in both sides operation correct situation will defeat.N Point: Win point, in this case, both sides operate correctly under the circumstances to win.the nature of winning and losing points: 1, all the endpoints are a must defeat P. (We use this as the basis for reasoning, in other words, we assume that) 2, from any win point n operation, there is at least one way to enter the mandatory point P. 3, regardless of operation, must defeat the point P can only enter the victory point N. We study the goal time of winning and losing points to simplify the problem and help our analysis. Usually we analyze both the winning and the losing points are analyzed in reverse order with the end point. us tohdu 1847 good Luck in CET-4 everybody!As an example: when n = 0 o'clock, it is obviously a must, because at this time you can not operate it when n = 1 o'clock, because you will be able to take all the cards at once, so when the victory point when n = 2 o'clock, also can be taken out, so when the win point when n = 3 o'clock, or one left or two, no matter how to take Will face the win point, so this point is a must defeat. And so on, you can get it at last; n:0 1 2 3 4 5 6 ... position:p n n p n N p ... What did you find out? No, yes, they are the rule, using the P/N to analyze, there is no problem has become simple. Now give you a slightly more complicated little:hdu 2147 Kiki ' s game
Now let's introduce the protagonist of today. Combining games is often complicated, but there is a new tool that makes combinatorial problems simple ———— the SG function and the SG theorem.
Sprague-grundy theorem (sg theorem):
the game and the SG function equals the Nim of each game SG function. This simplifies the problem by dividing each of the sub-games. The Bouton theorem is the direct application of the Sprague-grundy theorem in Nim games because the single-heap NIM game SG function satisfies the SG (x) = x. Do not know the Nim game please visit: here
SG function:
The MEX (minimal excludant) operation is defined first, which is an operation applied to a set that represents the smallest non-negative integer that does not belong to this set. such as Mex{0,1,2,4}=3, Mex{2,3,5}=0, mex{}=0.
For any state x, define SG (x) = Mex (s), where S is the set of the value of the SG function for the subsequent state of X. If X has three successive states of SG (a), SG (b), SG (c), then SG (x) = MEX{SG (a), SG (b), SG (c)}. Thus, the final state of the set S must be empty, so the final state of the SG function is SG (x) = 0, when and only if X is a must-fail p.
"Example" takes the stone problem
There are 1 heap of n stones, each time can only take {1, 3, 4} A stone, the first to take the stone winner, then the number of the SG value?
sg[0]=0,f[]={1,3,4},
X=1, you can take away 1-f{1} stones, the remaining {0}, so sg[1] = mex{sg[0]}= mex{0} = 1;
x=2, you can take away 2-f{1} stones, the remaining {1}, so sg[2] = mex{sg[1]}= Mex{1} = 0;
X=3, you can take away 3-f{1,3} stones, remaining {2,0}, so sg[3] = mex{sg[2],sg[0]} = mex{0,0} = 1;
X=4, you can take away 4-f{1,3,4} stones, remaining {3,1,0}, so sg[4] = mex{sg[3],sg[1],sg[0]} = mex{1,1,0} = 2;
X=5, you can take away 5-f{1,3,4} stones, remaining {4,2,1}, so sg[5] = mex{sg[4],sg[2],sg[1]} =mex{2,0,1} = 3;
And so on .....
x012345678....
SG[X] 010123201....
From the above example we can get the SG function value solution Step, then calculate the 1~n SG function value step as follows:
1. Use the array F to record the way the current state can be changed.
2. Then we use another array to mark the subsequent state of the current state X.
3, the last simulation of the Mex operation, that is, we are in the tag value of the search for the smallest value of the unmarked value, it is assigned to the SG (x).
4, we continue to repeat 2-3 of the steps, completed the calculation of 1~n function values.
The code is implemented as follows:
F[n]: The way in which the current state can be changed, N is the kind of mode, F[n] to pre-GETSG the//sg[]:0~n SG function value//s[]: Set int f[n],sg[maxn],s[maxn];void for x successor states GETSG (int n) { int i,j; memset (sg,0,sizeof (SG)); Since sg[0] is always equal to 0, I start from 1 for (i = 1; I <= n; i++) { //each time the successor set of the previous state is reset memset (s,0,sizeof (S)); for (j = 0; F[j] <= i && J <= N; j + +) S[sg[i-f[j]] = 1; The value of the SG function of the successor State is marked for (j = 0;; j + +) if (! S[j]) { //query the minimum non-0 value of the current successor status SG value sg[i] = j; Break;}}}
Now let's take a practical walkthrough ( topic Link ):
Just follow the above thinking, to solve this is divided into minutes of the problem.
The code is as follows:
#include <stdio.h> #include <string.h> #define MAXN + 10#define N 20int f[n],sg[maxn],s[maxn];void GETSG ( int n) { int i,j; memset (sg,0,sizeof (SG)); for (i = 1; I <= n; i++) { memset (s,0,sizeof (S)); for (j = 0; F[j] <= i && J <= N; j + +) S[sg[i-f[j]] = 1; for (j = 0;;; J + +) if (! S[j]) { sg[i] = j; Break;}}} int main () { int n,m,k; F[0] = f[1] = 1; for (int i = 2; I <=; i++) f[i] = f[i-1] + f[i-2]; GETSG (+); while (scanf ("%d%d%d", &m,&n,&k), m| | n| | K) { if (Sg[n]^sg[m]^sg[k]) printf ("fibo\n"); else printf ("nacci\n"); } return 0;}
Whether people are not satisfied, then I will be attached to some of the topics of combinatorial game:
POJ 2234 Matches Game
Hoj 2533 Stone II
POJ 2975 Nim
Hoj 1367 A Stone Game
POJ 2505 A Multiplication Game
Zju 3057 Beans Game
POJ 1067 take the stone game
POJ 2484 A Funny Game
POJ 2425 A Chess Game
POJ 2960 S-nim
POJ 1704 Georgia and Bob
POJ 1740 A New Stone Game
POJ 2068 Nim
POJ 3480 John
POJ 2348 Euclid ' s Game
Hoj 2645 Wnim
POJ 3710 Christmas Game
POJ 3533 Light Switching Game
(If there is an error, please correct it, reproduced the source)
Combined game-SG function and SG theorem