HDU 1848 Fibonacci again and again

题解:尼姆博弈,对于1至1000计算SG函数,每次取最小的前继值,SG值异或为0则为P-position。

#include <cstdio>  
#include <cstring> 
using namespace std;  
int fbi[30];  
int SG[1001];  
int m,n,p;  
int main(){  
    fbi[0]=0,fbi[1]=1,fbi[2]=2;  
    for(int i=3;fbi[i-1]<=1000;i++)fbi[i]=fbi[i-1]+fbi[i-2];  
    memset(SG,0,sizeof SG);  
    int hash[40];  
    for(int i=1;i<=1000;i++){   
        memset(hash,0,sizeof hash);  
        for(int j=1;fbi[j]<=i;j++)hash[SG[i-fbi[j]]]=1;  
        for(int j=0;;j++)if(hash[j]==0){SG[i]=j;break;}       
    }  
    while(scanf("%d%d%d",&m,&n,&p),m||n||p){  
        if((SG[m]^SG[n]^SG[p])!=0)printf("Fibo\n");  
        else printf("Nacci\n");  
    }  
    return 0;  
}
posted @ 2014-04-20 13:51  forever97  阅读(185)  评论(0编辑  收藏  举报