题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848
1 1 1 1 4 1 0 0 0
Fibo Nacci
代码如下:
#include <cstdio> #include <cstring> int x[1056], SG[1056]; int vis[1056]; void getSG(int n) { memset(SG,0,sizeof(SG)); x[0] = x[1] = 1; for(int i = 2; ; i++) { x[i] = x[i-1] + x[i-2]; if(x[i] > 1000) break; } SG[0] = 0; for(int i = 1; i <= n; i++)//get Sprague-Grundy value; { memset(vis,0,sizeof(vis)); for(int j = 0; x[j] <= i; j++) { vis[SG[i-x[j]]] = 1; } for(int j = 0; j <= n; j++)//求mex{}中未出现的最小的非负整数 { if(!vis[j]) { SG[i] = j; break; } } } } int main () { int m, n, p; getSG(1047); while (~scanf("%d%d%d",&m,&n,&p)) { if(m==0 && n==0 && p==0) break; if((SG[m]^SG[n]^SG[p]) == 0) printf("Nacci\n"); else printf("Fibo\n"); } return 0; }