The following two templates should be more rigorous, the inside of the f[] is zero-based.
Reprint Source: Transfer from: http://blog.csdn.net/primoblog/article/details/13376057
1. SG Play table
1 //f[]: The number of stones that can be taken away2 //sg[]:0~n value of SG function3 //hash[]:mex{}4 intF[k],sg[n],hash[n]; 5 voidGETSG (intN)6 { 7memset (SG,0,sizeof(SG)); 8 for(intI=1; i<=n; i++) { 9memset (Hash,0,sizeof(hash)); Ten for(intj=0; F[j]<=i && J < K; J + +)//k is the effective length of f[] Onehash[sg[i-f[j]]]=1; A for(intj=0; ; J + +) {//The smallest non-negative integer that is not present in mes{} - if(hash[j]==0) { -sg[i]=J; the Break; - } - } - } +}
2dfs
//Note that the S array to sort by from small to large the SG function is initialized to-1 for each collection simply initialize 1 times//N is the size of the set S S[i] is an array of special rules that are defined intS[n],sg[n],n; intGETSG (intx) {if(sg[x]!=-1) returnSg[x]; BOOLVis[m]; memset (Vis,0,sizeof(VIS)); for(intI=0; i<n; i++) { if(x>=S[i]) VIS[GETSG (x-s[i])]=1; } for(i=0;; i++) if(!Vis[i]) {Sg[x]=i; Break; } returnSg[x]; }
SG Game function template