1 1 1 1 4 1 0 0 0
Fibo Nacci
经典的SG 博弈。
我们算出sg的值。,用三堆石头的sg值异或,假设为0则后手胜,不为0则先手胜。
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <iostream>
#include <cstdio>
using namespace std;
int hash1[1005];
int sg[1005];
int f[1005];
void getsg(int x)
{
int i,j;
memset(sg,0,sizeof(sg));
for(i=1; i<=x; i++)
{
memset(hash1,0,sizeof(hash1));
for(j=1; f[j]<=i; j++)
{
hash1[sg[i-f[j]]]=1;
}
for(j=0; j<=x; j++)
{
if(!hash1[j])
{
sg[i]=j;
break;
}
}
}
}
int main()
{
f[1]=1;
f[2]=2;
for(int i=3;; i++)
{
f[i]=f[i-1]+f[i-2];
if(f[i]>1000)
break;
}
int m,n,p;
getsg(1000);
while(cin>>m>>n>>p,m,n,p)
{
if((sg[m]^sg[n]^sg[p])==0)
cout<<"Nacci"<<endl;
else
cout<<"Fibo"<<endl;
}
return 0;
}
HDU 1848(sg博弈) Fibonacci again and again
原文:http://www.cnblogs.com/llguanli/p/7106898.html