HDOJ1848NIM博弈 SG函数
Posted randy-lo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ1848NIM博弈 SG函数相关的知识,希望对你有一定的参考价值。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848
代码如下:
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; #define pf printf #define mem(a,b) memset(a,b,sizeof(a)) #define prime1 1e9+7 #define prime2 1e9+9 #define pi 3.14159265 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define scand(x) scanf("%llf",&x) #define f(i,a,b) for(int i=a;i<=b;i++) #define scan(a) scanf("%d",&a) #define mp(a,b) make_pair((a),(b)) #define P pair<int,int> #define dbg(args) cout<<#args<<":"<<args<<endl; #define inf 0x7ffffff inline int read(){ int ans=0,w=1; char ch=getchar(); while(!isdigit(ch)){if(ch==‘-‘)w=-1;ch=getchar();} while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-‘0‘,ch=getchar(); return ans*w; } int n,m,p; const int maxn=1005; const ll mod=10000; int fib[]={1,2,3,5,8,13,21,34,55,89,144,233,377,610,987}; int sg[maxn+1],s[maxn+1]; void getSG(){ for(int i=0;i<=maxn;i++){ sg[i]=i; mem(s,0); for(int j=0;j<15&&i-fib[j]>=0;j++){ s[sg[i-fib[j]]]=1;//i的后继结点放到s中 for(int k=0;k<=i;k++) if(!s[k]){sg[i]=k;break;} //最小的等于0的索引 } } } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); std::ios::sync_with_stdio(false); getSG(); while(scanf("%d%d%d",&m,&n,&p)&&!(m==0&&n==0&&p==0)){ if(sg[m]^sg[n]^sg[p])cout<<"Fibo "; else cout<<"Nacci "; } }
以上是关于HDOJ1848NIM博弈 SG函数的主要内容,如果未能解决你的问题,请参考以下文章
HDU.1848.Fibonacci again and again(博弈论 Nim)
HDU 1848 Fibonacci again and again (sg函数,博弈论)
博弈论 SG函数(模板) HDU 1848 Fibonacci again and again