【poj2960】 S-Nim

http://poj.org/problem?id=2960 (题目链接)

题意:经典Nim游戏,只是给出了一个集合S,每次只能取S[i]个石子。

Solution
  g(x)=mex{SG(x-s[1]),SG(x-s[2])……}
  数据范围很小,可以暴力求SG,顺便记忆化一下。不知道为什么用map就TLE了。。。只好开数组了。

代码:

// poj2960
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<map>
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
    LL x=0,f=1;char ch=getchar();
    while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

const int maxn=10010;
int k,n,m,sg[maxn],s[maxn];

void calsg(int x) {
    if (sg[x]!=-1) return;
    //map<int,bool> mark;
    bool mark[10010];
    memset(mark,0,sizeof(mark));
    int temp;
    for (int i=1;i<=k;i++) {
        temp=x-s[i];
        if (temp>=0) {
            if (sg[temp]==-1) calsg(temp);
            mark[sg[temp]]=1;
        }
    }
    for (int i=0;;i++) if (!mark[i]) {sg[x]=i;return;}
}
int main() {
    while (scanf("%d",&k)!=EOF && k) {
        for (int i=1;i<=k;i++) scanf("%d",&s[i]);
        sg[0]=0;
        for (int i=1;i<=maxn;i++)
            sg[i]=-1;
        scanf("%d",&m);
        while (m--) {
            scanf("%d",&n);
            int ans=0;
            while (n--) {
                int x;scanf("%d",&x);
                if (sg[x]==-1) calsg(x);
                ans^=sg[x];
            }
            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、付费专栏及课程。

余额充值