hdu 1848 hdu 1536 博弈论sg函数入门

hdu 1848:

题意:

给三堆石子,数量分别是 m,n,p,每次取只能取fib数个石子,求最后谁赢。


解析:

学习:

点击打开链接

sg函数的应用。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

int fib[100];
void fibTable()
{
    fib[0] = fib[1] = 1;
    for (int i = 2; i < 20; i++)
    {
        fib[i] = fib[i - 1] + fib[i - 2];
    }
}

int sg[1010];
void sgTable()
{
    sg[0] = 0;
    for (int j = 1; j <= 1000; j++)
    {
        set<int> s;
        for (int i = 0; i < 20; i++)
        {
            if (fib[i] <= j)
            {
                s.insert(sg[j - fib[i]]);
            }
        }
        int t = 0;
        while (s.count(t))
            t++;
        sg[j] = t;
    }
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAl
    fibTable();
    sgTable();
    int m, n, p;
    while (~scanf("%d%d%d", &m, &n, &p))
    {
        if (!m && !n && !p)
            break;
        int ans = sg[m] ^ sg[n] ^ sg[p];
        if (ans)
            puts("Fibo");
        else
            puts("Nacci");
    }
    return 0;
}

hdu 1536:

sg dfs 模板:

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 10000 + 10;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double ee = exp(1.0);

int a[110];
int sg[maxn];
int n;

int sgDfs(int x)
{
    if (sg[x] != -1)
        return sg[x];
    bool vis[110];
    memset(vis, false, sizeof(vis));
    for (int i = 0; i < n; i++)
    {
        if (a[i] <= x)
        {
            sgDfs(x - a[i]);
            vis[sg[x - a[i]]] = true;
        }
    }
    int res = 0;
    while (vis[res])
        res++;
    return sg[x] = res;
}


int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAl
    while (~scanf("%d", &n) && n)
    {
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
        }

        sort(a, a + n);
        memset(sg, -1, sizeof(sg));

        int m;
        scanf("%d", &m);
        while(m--)
        {
            int ans = 0;
            int k;
            scanf("%d", &k);
            while (k--)
            {
                int t;
                scanf("%d", &t);
                ans ^= sgDfs(t);
            }
            if (ans)
                printf("W");
            else
                printf("L");
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值