題目
變種的尼姆博弈,可以通過SG函數轉化爲尼姆博弈進行解決
題解
先求出斐波那契數列,然後求出相應的SG函數,又因爲是三個堆,所以根據解決尼姆博弈的方法將SG函數值進行異或,得到答案
代碼
#include<iostream>
#include<cstring>
using namespace std;
const int Maxm=1010,Maxn=10;
int f[20],sg[Maxm];
bool Hash[Maxm];
void Getsg()
{
f[0]=1;f[1]=1;
for(int i=2;i<=16;i++){
f[i]=f[i-1]+f[i-2];
}
for(int i=1;i<Maxm;i++){
//cout<<"i:"<<i<<endl;
memset(Hash,false,sizeof(Hash));
for(int j=1;j<=16&&f[j]<=i;j++){
Hash[sg[i-f[j]]]=true;
//cout<<sg[i-f[j]]<<' ';
}
//cout<<endl;
for(int j=0;j<=i;j++){
if(!Hash[j]){
sg[i]=j;
//cout<<j<<endl;
break;
}
}
}
}
int main()
{
Getsg();
int m1,m2,m3;
while(scanf("%d%d%d",&m1,&m2,&m3)==3&&m1){
if((sg[m1]^sg[m2]^sg[m3])!=0){
cout<<"Fibo"<<endl;
}
else{
cout<<"Nacci"<<endl;
}
}
}