Although this article is reproduced, but the code and the original is not the same, I think my code is better.
Reprinted from:http://www.cnblogs.com/frog112111/p/3199780.html
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 a given, forward-free graph, define the Sprague-grundy function g for each vertex of the graph as follows: g (x) =mex{g (y) | Y is the successor of X, where G (x) is sg[x]
For example: Take the stone problem, there are 1 heap n stone, at a time can only take {1,3,4} a stone, first 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}, mex{sg[0]}={0}, so sg[1]=1;
x=2, you can take away 2-f{1} stones, the remaining {1}, mex{sg[1]}={1}, so sg[2]=0;
X=3, you can take away 3-f{1,3} stones, the remaining {2,0}, mex{sg[2],sg[0]}={0,0}, so sg[3]=1;
X=4, you can take away 4-f{1,3,4} stones, the remaining {3,1,0}, mex{sg[3],sg[1],sg[0]}={1,1,0}, so sg[4]=2;
X=5, you can take away 5-f{1,3,4} stones, the remaining {4,2,1}, mex{sg[4],sg[2],sg[1]}={2,0,1}, so sg[5]=3;
And so on .....
x012345678....
SG[X] 010123201....
Calculates the SG value from the 1-n range.
F (the number of steps that can be walked, f[0] indicates how many ways to go)
F[] need to sort from small to large
1. The number of steps can be 1~m continuous integer, directly modulo, SG (x) = x (m+1);
2. The optional step is any step, SG (x) = x;
3. The optional number of steps is a series of discontinuous numbers, calculated with GETSG ()
SG Play Table Template:
//the value of the SG function for [1,n]//F[0]: Number of scenarios that can be taken f[1]~f[n] The number of stones each scheme can take//sg[]:0~n SG function value hash[]: In order to find the smallest nonnegative integerConst intN = ++5 ;intF[n], sg[n], hash[n];voidSgsol (intN) { intI, J; memset (SG,0,sizeof(SG)); for(i =1; I <= N; i++) {memset (Hash,0,sizeof(Hash)); //This j is less than f[0], because only f[0] kind of situation for(j =1; F[J] <= i && J <= f[0]; J + +) Hash[sg[i-F[J]] =1; //The smallest non-negative integer that is not present in mes{} for(j =0;; J + +) { if(Hash[j] = =0) {Sg[i]=J; Break; } } }}
HDU 1848
Test instructions
Take the stone problem, a total of 3 stones, each time only to take the Fibonacci number of stones, first take the stone to win, ask the winner or the next win
Exercises
First put Fibonacci into f[], for 1000 of the SG value, then 3 different or better.
SG function template