hdu 1848 Fibonacci again and again SG打表

来源:互联网 发布:手机单机游戏推荐 知乎 编辑:程序博客网 时间:2024/05/20 00:09

      三堆石子,两人轮流取,每次选一堆,取走任意斐波纳契数量的石子,取完最后一个石子的胜利,求先手胜负。

取子的方案都给了,直接打表求sg吧。

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>using namespace std;typedef long long ll;int n,m;int sg[10100];int fib[1010];int ct;int mex(int x){    bool g[1010];    int t;    memset(g,0,sizeof g);    for (int i=1; i<=ct; i++)    {        t=x-fib[i];        if (t<0) break;        if (sg[t]==-1)  sg[t]=mex(t);        g[sg[t]]=true;    }    for (int i=0; ; i++)    if (!g[i]) return i;}int main(){    fib[0]=fib[1]=1;    for (int i=2; fib[i-2]+fib[i-1]<=1000; i++)    {        fib[i]=fib[i-2]+fib[i-1];        ct=i;    }    memset(sg,-1,sizeof sg);    for (int i=0; i<=1000; i++)    sg[i]=mex(i);    int x,y,z;    while(scanf("%d%d%d",&x,&y,&z))    {        if (!x && !y && !z) break;        int ans=sg[x]^sg[y]^sg[z];        if (ans) puts("Fibo");        else puts("Nacci");    }    return 0;}



0 0
原创粉丝点击