SG函数和SG定理
Fibonacci again and again
利用SG函数求出每一堆的SG值,如果三个值的异或和为零 先手必败态,否则,先手必胜态。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 #define scanf_d(a) scanf("%d",&a) 3 #define scanf_dd(a,b) scanf("%d%d",&a,&b) 4 #define maxn 1000+10 5 #define N 20 6 //#define DEBUG 7 using namespace std; 8 int s[maxn],sg[maxn],fib[N]; 9 10 void getsg( int n) { 11 memset(sg,0,sizeof(sg)); 12 for ( int i = 1;i <= n; i++) { 13 memset(s,0,sizeof(s)); 14 for ( int j = 0; fib[j] <= i && j <= N; j++) s[sg[i-fib[j]]]=1; 15 for ( int j = 0; ; j++) { if(!s[j]) { sg[i] = j; break; } } 16 } 17 } 18 19 int main() 20 { 21 #ifdef DEBUG 22 freopen("Text.txt","r",stdin); 23 #endif // DEBUG 24 int n,m,k; 25 fib[0] = fib[1] = 1; 26 for( int i = 2; i <= 16; i++) fib[i] = fib[i-1] + fib[i-2]; 27 getsg(1000); 28 while(~scanf("%d%d%d",&n,&m,&k),m||n||k) { 29 if(sg[n]^sg[m]^sg[k]) puts("Fibo"); 30 else puts("Nacci"); 31 } 32 return 0; 33 }
MILE NEVER GIVE UP!!!