HDU 1536 S-Nim 博弈,SG函数

HDU 1536 S-Nim

博弈,SG函数

传送门:HDU


题意

取石子游戏。
先给你一个数组长度k,然后是长度为k的数组。表示合法的取石子个数Si。
然后是n,表示n个询问。
每个询问有一个m,m堆石子,每堆mi个。每次操作允许在某一堆石子中取出Si个石子。问你当前状态谁最后赢。


思路

算是SG函数入门题吧。
两篇对我理解有帮助的博客:1。2。


代码

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

const int MAXN=10007;
const int oo=0x3f3f3f3f;
typedef long long LL;
const LL loo=4223372036854775807ll;
typedef long double LB;
int sg[MAXN], s[MAXN];
int get_SG(int x,int n)
{
    if(sg[x]!=-1) return sg[x];
    bool vis[MAXN];
    memset(vis, 0, sizeof(vis));
    for(int i=0;i<n;i++)
    {
        if(x>=s[i])
        {
            get_SG(x-s[i], n); 
            vis[sg[x-s[i]]]=1; 
        }
    }
    for(int i=0;;i++)
    {
        if(!vis[i]) { sg[x]=i;break; }
    }
    return sg[x];
}
int main()
{
    _
    int m;
    while(cin>>m)
    {
        if(m==0) break;
        for(int i=0;i<m;i++) cin>>s[i];
        memset(sg, -1, sizeof(sg));
        int n;cin>>n;
        string ss="";
        for(int i=0;i<n;i++)
        {
            int ans=0;
            int k=0;cin>>k;
            while(k--)
            {
                int tt;cin>>tt;
                int kkk=get_SG(tt, m);
                ans=ans^kkk;
                cout<<kkk<<endl;
            }
            if(ans==0) ss+='L';
            else ss+='W';
        }
        cout<<ss<<endl;
    }
    //system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值