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\n";
        else cout<<"Nacci\n";
    }

}

 

上一篇:leetcode做题总结


下一篇:LeetCode 第 193 场周赛 解题报告