HDU 1848 Fibonacci again and again(SG函数入门)题解

思路:SG打表

参考:SG函数和SG定理【详解】

代码:

#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define eps 1e-9
typedef long long ll;
const int maxn = 1e3 + 10;
const int seed = 131;
const ll MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
int fib[50];
int s[maxn], sg[maxn];
void getSG(){
    sg[0] = 0;
    for(int i = 1; i <= 1000; i++){
        memset(s, 0, sizeof(s));
        for(int j = 1; fib[j] <= i; j++){
            s[sg[i - fib[j]]] = 1;
        }
        for(int j = 0; j <= i; j++){
            if(!s[j]){
                sg[i] = j;
                break;
            }
        }
    }
}
void FIB(){
    fib[1] = 1, fib[2] = 2;
    for(int i = 3; i <= 30; i++)
        fib[i] = fib[i - 1] + fib[i - 2];
}
int main(){
    int n, m, p;
    FIB();
    getSG();
    while(scanf("%d%d%d", &n, &m ,&p) && n + m + p){
        if(sg[n] ^ sg[m] ^ sg[p]) printf("Fibo\n");
        else printf("Nacci\n");
    }
    return 0;
}

 

posted @ 2018-09-16 00:49  KirinSB  阅读(193)  评论(0编辑  收藏  举报