hdu 1848 Fibonacci again and again

Game theory SG function explanation

#include <iostream>  
#include <cstdio>  
#include <cstring>  
using namespace std;  
  
const int N = 1005;  
int f[N];//Number of stones that can be removed  
int sg[N];//The SG function value of 0~n  
int Hash[N];  
  
void getSG(int n){  
    memset(sg,0,sizeof(sg));  
    for(int i = 1; i <= n; i++){  
        memset(Hash,0,sizeof(Hash));  
        for(int j = 1; f[j] <= i; j++)  
            Hash[sg[i-f[j]]] = 1;  
        for(int j = 0; j <= n; j++){ //find the smallest non-negative integer that does not appear in mes{}  
            if(Hash[j] == 0){  
                sg[i] = j;  
                break;  
            }  
        }  
    }  
}  
  
int main(){  
    f[0] = f[1] = 1;  
    for(int i = 2; i <= 16; i++)  
        f[i] = f[i-1]+f[i-2];  
    getSG(1000);  
    int m,n,p;  
    while(scanf("%d%d%d",&m,&n,&p),m+n+p){  
        if(sg[m]^sg[n]^sg[p])  
            puts("Fibo");  
        else  
            puts("Nacci");  
    }  
    return 0;  
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325778124&siteId=291194637
Recommended