题目链接

\(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;
}

HDU.1536.S-Nim(博弈论 Nim)的更多相关文章

  1. HDU 3404 Switch lights 博弈论 nim积

    http://acm.hdu.edu.cn/showproblem.php?pid=3404 题目 http://www.doc88.com/p-5098170314707.html 论文 nim积在 ...

  2. HDU 3094 树上删边 NIM变形

    基本的树上删边游戏 写过很多遍了 /** @Date : 2017-10-13 18:19:37 * @FileName: HDU 3094 树上删边 NIM变形.cpp * @Platform: W ...

  3. POJ 3553 Light Switching Game 博弈论 nim积 sg函数

    http://poj.org/problem?id=3533 变成三维的nim积..前面hdu那个算二维nim积的题的函数都不用改,多nim积一次就过了...longlong似乎不必要但是还是加上了 ...

  4. (转载)Nim博弈论

    最近补上次参加2019西安邀请赛的题,其中的E题出现了Nim博弈论,今天打算好好看看Nim博弈论,在网上看到这篇总结得超级好的博客,就转载了过来. 转载:https://www.cnblogs.com ...

  5. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

  6. HDU.2149 Public Sale (博弈论 巴什博弈)

    HDU.2149 Public Sale (博弈论 巴什博弈) 题意分析 巴什博奕裸题 博弈论快速入门 代码总览 #include <bits/stdc++.h> using namesp ...

  7. HDU.1846 Brave Game (博弈论 巴什博弈)

    HDU.1846 Brave Game (博弈论 巴什博弈) 题意分析 巴什博奕裸题 博弈论快速入门 代码总览 include <bits/stdc++.h> using namespac ...

  8. LightOJ 1253 Misere NIM(反NIM博弈)

    Alice and Bob are playing game of Misère Nim. Misère Nim is a game playing on k piles of stones, eac ...

  9. hdu 3032 Nim or not Nim? 博弈论

     这题是Lasker’s Nim. Clearly the Sprague-Grundy function for the one-pile game satisfies g(0) = 0 and g( ...

随机推荐

  1. 【转】Python中的字符串与字符编码

    [转]Python中的字符串与字符编码 本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常 ...

  2. ubuntu16.04安装opencv2.4.13

    1.更新 sudo apt-get update sudo apt-get upgrade 2.安装关联库 2.1 搭建C/C++编译环境 sudo apt-get install build-ess ...

  3. HOG feature

  4. dubbo系列五、dubbo核心配置

    一.配置文件 1.生产者配置provider.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...

  5. 关于报错stale element reference: element is not attached to the page document处理

    1.现象 在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element ref ...

  6. 一张纸,折多少次和珠穆拉峰一样高(for if 和break)

  7. python中type、object与class之间关系(一切皆对象)

    object是最顶层基类 object是type的实例,而type又继承object type是自身的实例 >>> class Student: ... pass ... >& ...

  8. 使用ts-loader与webpack编译typescripts出现Module build failed: TypeError: Cannot read property 'afterCompile' of undefined

    解决方法 将ts-loader从4.0降低到3.1.1解决问题.是由于webpack和ts-loader版本不兼容造成的.

  9. java uitl

    Random类  //生成随机数 https://i.cnblogs.com/EditPosts.aspx?opt=1

  10. Laravel Form 表单的数据校验

    例如,要使用手机号加验证码的方式提供登录网站的功能,那么在处理前端提交的 form 表单时,就不得不对提交的手机号及验证码做基本的数据校验. 手写规则,非常浪费时间.使用 laravel 内置的 va ...