hdu 1848 sg博弈模版题

/*
计算从1-n范围内的SG值。
Array(存储可以走的步数,Array[0]表示可以有多少种走法)
Array[]需要从小到大排序

1.可选步数为1-m的连续整数,直接取模即可,SG(x) = x % (m+1);
2.可选步数为任意步,SG(x) = x;
3.可选步数为一系列不连续的数,用GetSG(计算)
*/

int SG[MAX], hash[MAX];
void GetSG(int Array[], int n = MAX-1)
{
    int i, j;
    memset(SG, 0, sizeof(SG));
    for(i = 0; i <= n; i++)
    {
        memset(hash, 0, sizeof(hash));
        for(j = 1; Array[j]<=i; j++)
            hash[SG[i - Array[j]]] = 1;
        for(j = 0; j <= n; j++)
        {
            if(hash[j] == 0)
            {
                SG[i] = j;
                break;
            }
        }
    }
}

 

View Code
// I'm lanjiangzhou
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
#include <map>
#include <vector>
#include <deque>
#include <set>
using namespace std;

//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif

//**************************CONSTANT***********************
#define INF 0x3f3f3f3f

// aply for the memory of the stack
//#pragma comment (linker, "/STACK:1024000000,1024000000")
//end

/*const int maxn =1010;
int k;
int fibo[maxn],f[maxn];
int mex(int p){
    int t;
    bool g[110]={0};
    for(int i=0;i<k;i++){
        t=p-fibo[i];
        if(t<0) break;
        if(f[t]==-1) f[t]=mex(t);
        g[f[t]]=1;
    }
    for(int i=0;;i++){
        if(!g[i])
            return i;
    }
}

int main(){
    int n,m,p,s;
    fibo[0]=1;
    fibo[1]=2;
    for(int i=2;i<=18;i++){
        fibo[i]=fibo[i-1]+fibo[i-2];
    }
    k=19;
    sort(fibo,fibo+k);
    memset(f,0,sizeof(f));
    f[0]=0;
    for(int i=1;i<=1000;i++){
        f[i]=mex(i);
    }
    while(scanf("%d%d%d",&m,&n,&p)!=EOF){
        if(m==0&&n==0&&p==0) break;
        s=0;
        s=s^f[m]^f[n]^f[p];
        if(s==0) {
            printf("Nacci\n");
        }
        else {
            printf("Fibo\n");
        }
    }
    return 0;
}*/

const int maxn =1010;
int SG[maxn],hash[maxn];
int fibs[maxn];

void Fib(){
    fibs[1]=1;
    fibs[2]=2;
    for(int i=3;i<=18;i++){
        fibs[i]=fibs[i-1]+fibs[i-2];
    }
    return;
}

void GetSG(){
    memset(SG,0,sizeof(SG));
    for(int i=0;i<=maxn;i++){
        memset(hash,0,sizeof(hash));
        for(int j=1;fibs[j]<=i;j++){
            hash[SG[i-fibs[j]]]=1;
        }
        for(int j=0;j<=maxn;j++){
            if(hash[j]==0){
                SG[i]=j;
                break;
            }
        }
    }
    return;
}


int main(){
    Fib();
    GetSG();
    int n,m,p;
    while(scanf("%d%d%d",&n,&m,&p)!=EOF){
        if(n==0&&m==0&&p==0) break;
        if(SG[m]^SG[n]^SG[p]){
            printf("Fibo\n");
        }
        else {
            printf("Nacci\n");
        }
    }
}

 

posted @ 2013-04-16 00:50  南下的小程序员  阅读(203)  评论(0编辑  收藏  举报