HDU 1848 Fibonacci again and again

#include<stdio.h>

#include<string.h>

#include<stdlib.h>



const int MAXN = 1005;

int fib[MAXN], e[MAXN], h[20];



void calcFib()

{

    int i;

    fib[1] = 1, fib[2] = 2;

    for(i = 3; i <= 16; i ++)

    {

        fib[i] = fib[i - 1] + fib[i - 2];

    }

}



/*求SG值*/

void calcE()

{

    int i, j, k;

    e[0] = 0, e[1] = 1;

    for(i = 2; i <= 1000; i ++)

    {

        memset(h, 0, sizeof h);

        for(j = 1; fib[j] <= i; j ++)

        {

            h[e[i - fib[j]]] = 1;

        }

        for(j = 0; j <= 15; j ++)

        {

            if(h[j] == 0) //当j为必败态

            {

                e[i] = j;

                break;

            }

        }

    }

}



int main()

{

    int m, n, p;

    calcFib();

    calcE();

    while(scanf("%d%d%d", &m, &n, &p), m || n || p)

    {

        puts(((e[m] ^ e[n] ^ e[p]) == 0) ? "Nacci" : "Fibo"); //异或为0,先手必败

    }

    return 0;

}

 

你可能感兴趣的:(fibonacci)