HDU.1536.S-Nim(博弈论 Nim)

题目链接

\(Description\)

给定一个集合S,每次只能拿S中某个元素个数的石子。每组数据有多组询问,询问给出m堆石子个数,问先手是否必胜。有多组数据。

1.
首先对操作数组排个序,再预处理求sg就好了。

//530MS 1544K
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=10002;

int n,A[N],sg[N+3];
bool vis[N+3];

inline int read()
{
    int now=0;register char c=gc();
    for(;!isdigit(c);c=gc());
    for(;isdigit(c);now=now*10+c-'0',c=gc());
    return now;
}
void Init_SG()
{
    for(int i=1; i<N; ++i)
    {
        memset(vis,0,sizeof vis);
        for(int j=1; j<=n&&A[j]<=i; ++j)
            vis[sg[i-A[j]]]=1;
        for(int j=0; ; ++j)
            if(!vis[j]) {sg[i]=j; break;}
    }
}

int main()
{
    while(n=read(),n)
    {
        for(int i=1; i<=n; ++i) A[i]=read();
        std::sort(A+1,A+1+n);
        Init_SG();
        int m=read();
        while(m--)
        {
            int mx=read(),res=0;
            for(int i=1; i<=mx; ++i) res^=sg[read()];
            putchar(res?'W':'L');
        }
        putchar('\n');
    }
    return 0;
}

2.
首先对操作数组排个序。对于每个需要的sg值可以记忆化求。
就是每次递归需要开个vis[N]比较。。

//374MS 13072K
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=10002;

int n,A[N],sg[N+3];

inline int read()
{
    int now=0;register char c=gc();
    for(;!isdigit(c);c=gc());
    for(;isdigit(c);now=now*10+c-'0',c=gc());
    return now;
}
int Get_SG(int x)
{
    if(sg[x]!=-1) return sg[x];
    bool vis[N+3];
    memset(vis,0,sizeof vis);
    for(int j=1; j<=n&&A[j]<=x; ++j)
        vis[Get_SG(x-A[j])]=1;
    for(int j=0; ; ++j)
        if(!vis[j]) return sg[x]=j;
}

int main()
{
    while(n=read(),n)
    {
        for(int i=1; i<=n; ++i) A[i]=read();
        std::sort(A+1,A+1+n);
        memset(sg,0xff,sizeof sg);
        int m=read();
        while(m--)
        {
            int mx=read(),res=0;
            for(int i=1; i<=mx; ++i) res^=Get_SG(read());
            putchar(res?'W':'L');
        }
        putchar('\n');
    }
    return 0;
}

转载于:https://www.cnblogs.com/SovietPower/p/8468556.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值