hdu 1848 S-G函数

#include <cstdio>
#include <cstring>
using namespace std;
int SG[1010],f[1010],cnt;
bool S[1010];

void get_SG(){
	for(int i=1;i<=1000;i++){
		memset(S,0,sizeof(S));
		for(int j=0;f[j]<=i && j<=cnt;j++)S[SG[i-f[j]]]=1;
		for(int j=0;;j++)
			if(!S[j]){SG[i]=j; break;}
	}
}

int main(){
	f[0]=1,f[1]=1,f[2]=2; cnt=2;
	while(1){
		f[cnt]=f[cnt-1]+f[cnt-2];
		if(f[cnt]>1000)break;
		cnt++;
	}
	get_SG();
	int n,m,p;
	while(scanf("%d%d%d",&n,&m,&p) && (n||m||p)){
		if((SG[n]^SG[m]^SG[p])==0)puts("Nacci");
		else puts("Fibo");
	}
	return 0;
} 

  

原文地址:https://www.cnblogs.com/codetogether/p/13362755.html