HDU 1848 Fibonacci again and again

HDU_1848

    用记忆化搜索的方式处理出sg函数值,然后看sg[n]^sg[m]^sg[p]是否为0即可,推荐一个讲SG函数入门知识的博客:http://www.cnblogs.com/Knuth/archive/2009/09/05/1561007.html

#include<stdio.h>
#include<string.h>
#define MAXD 1010
int f[20], h[MAXD][20], sg[MAXD];
void prepare()
{
int i;
f[0] = f[1] = 1;
for(i = 2; i <= 15; i ++)
f[i] = f[i - 1] + f[i - 2];
memset(h, 0, sizeof(h));
memset(sg, -1, sizeof(sg));
}
int dfs(int n)
{
int i;
if(sg[n] != -1)
return sg[n];
for(i = 1; i <= 15; i ++)
if(n - f[i] >= 0)
h[n][dfs(n - f[i])] = 1;
for(i = 0; h[n][i]; i ++);
return sg[n] = i;
}
int main()
{
prepare();
for(;;)
{
int m, n, p;
scanf("%d%d%d", &m, &n, &p);
if(!m && !n && !p)
break;
printf("%s\n", (dfs(m) ^ dfs(n) ^ dfs(p)) == 0 ? "Nacci" : "Fibo");
}
return 0;
}


你可能感兴趣的:(fibonacci)