For the Nim game, any singular situation (a,b,c) has a^b^c=0.
Extension: Any singular situation (A1, A2,... an) satisfies a1^a2^...^an=0
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-garundy function g for each vertex of the graph as follows:
g (x) =mex{g (y) | Y is the successor of X.
SG Function Properties:
1, all endpoints SG value is 0 (because its successor is an empty set)
2,SG is a vertex of 0, and all subsequent y of it satisfies SG not to be 0
3, for a SG not 0 vertex, there must be a successor to meet the SG is 0
4, meet the combined game nature: All SG is 0 fixed point corresponding P point, SG greater than 0 vertices corresponding to n points
sg theorem (Sprague-grundy theorem):
G (g) =g (G1) ^g (G2) ^...^g (Gn). The game's and SG function values are the XOR of all its sub-games ' SG function values.
#include <stdio.h>#include<string.h>Const intn=1111;intp[n],sg[n],f[ +];voidinit () {inti,j; f[1]=1, f[2]=2; for(i=3;; i++) {F[i]=f[i-2]+f[i-1]; if(f[i]>n) Break; } for(i=1; i<= +; i++){ for(j=1; f[j]<=i;j++) {P[sg[i-f[j]]]=i; } for(j=0; j<=i;j++){ if(p[j]!=i) {Sg[i]=K; Break; } } }}intMain () {init (); intn,m,p; while(~SCANF ("%d%d%d",&n,&m,&p)) { if(!n&&!m&&!p) Break; if(Sg[n]^sg[m]^sg[p]) puts ("Fibo"); ElsePuts"Nacci"); } return 0;}
HDU 1848 Fibonacci again and again "SG function"