HDU1848 Fibonacci again and again(SG函数的应用)
2016-04-21 21:45
549 查看
学习自SG函数讲解http://blog.sina.com.cn/s/blog_83d1d5c70100y9yd.html
上代码
不懂的地方请留言。
上代码
#include<iostream> #include<string> #include<queue> #include<cstring> #include<algorithm> #include<cmath> #include<cstdio> int book[1005]; using namespace std; int f[1005],sg[1005]; int main() { int a,b,c; f[0]=1; f[1]=1; for(int i=2;i<=50;i++)f[i]=f[i-1]+f[i-2]; sg[0]=0;//前几项自己预处理算一下 sg[1]=1; sg[2]=2; //由于数据只有1005 直接暴力sg表就可以了 for(int i=3;i<1005;i++)//SG公式 g(x) = mex{g(y) : y∈F(x)} { int ans=100000000; int tmax=0; memset(book,0,sizeof(book));//用于挑选出mex{.......} for(int j=0;f[j]<=i;j++) { book[sg[i-f[j]]]=1; tmax=max(sg[i-f[j]],tmax); } for(int k=0;k<=tmax+1;k++)//用于挑选出mex{.......} { if(book[k]==0) { sg[i]=k; break; } } } //for(int i=0;i<1005;i++)cout<<sg[i]<<" "; //cout<<f[20]<<endl; while(cin>>a>>b>>c&&!(a==0&&b==0&&c==0)) { if((sg[a]^sg[b]^sg[c])!=0)cout<<"Fibo"<<endl; else cout<<"Nacci"<<endl; } return 0; }
不懂的地方请留言。
相关文章推荐
- waitKey()
- SDUT 2877:angry_birds_again_and_again
- LightOJ 1076 Get the Containers
- 【Light】[1076 ]Get the Containers
- 阶梯博弈(Staircase Nim)
- Dispatcher initialization failed:Unable to load configuration
- light oj 1076 - Get the Containers
- 哈佛大学研究新人工智能系统 速度媲美人类大脑
- HDU 2820 Permutaion
- Fibonacci Again
- POJ3691.DNA repair (Trie图 && DP)
- com.google.android.xts.devicepolicy.DeviceOwnerTest#testFactoryResetProtection fail
- AIDL API级别详细解释
- BZOJ 3172 [Tjoi2013]单词 AC自己主动机(fail树)
- linker command failed with exit code 1 (use -v to see
- ping: unknown host www.baidu.com 最简单的方法
- 严重: StandardServer.await: create[localhost:8005]
- HDU 5319 Painter
- A. Reconnaissance 2
- A. Reconnaissance