题目链接

\(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数据分析入门 最近,Analysis with Programming加入了Planet Python.作为该网站的首批特约博客,我这里来分享一下如何通过Python来开始数据分析.具体内 ...

  2. 网络抓包 Fiddler

    1. Fiddler 抓包简介 Fiddler是通过改写HTTP代理,让数据从它那通过,来监控并且截取到数据.当然Fiddler很屌,在打开它的那一瞬间,它就已经设置好了浏览器的代理了.当你关闭的时候 ...

  3. linux windows 共享文件夹

    1.首先在windows上共享一个目录,如:共享了目录share,用户和密码都是:massky 2.在linux机器上,在/mnt目录下建立一个ml45目录,使用root用户,执行下面命令: moun ...

  4. linux 平台core dump文件生成

    1. 在终端中输入ulimit -c 如果结果为0,说明当程序崩溃时,系统并不能生成core dump. root@hbg:/# ulimit -c0root@hbg:/# 2.使用ulimit -c ...

  5. linux块设备驱动

    块设备驱动程序<1>.块设备和字符设备的区别1.读取数据的单元不同,块设备读写数据的基本单元是块,字符设备的基本单元是字节.2.块设备可以随机访问,字符设备只能顺序访问. 块设备的访问:当 ...

  6. Ubuntu下安装Golang并测试HelloWorld

    Intel Core i5-8250U,Ubuntu 18.04(安装在虚拟机Oracle VirtualBox 5.2.12上),Go 1.11, 安装步骤如下: -进入Go文档官网: https: ...

  7. iOS 8 WKWebView 知识点

    首先看看这篇文章,写得很好:http://nshipster.cn/wkwebkit/ 再推荐去看看 iOS_8_by_Tutorials 这本书里的 WKWebView相关章节! 我这里说下自己的简 ...

  8. jQuery版本的jsonp

    1.一个众所周知的问题,Ajax直接请求普通文件存在跨域无权限访问的问题,甭管你是静态页面.动态网页.web服务.WCF,只要是跨域请求,一律不准: 2.不过我们又发现,Web页面上调用js文件时则不 ...

  9. java多线程快速入门(六)

    多线程应用实例(批量发送短信) 1.创建实体类 package com.cppdy; public class UserEntity { private int id; private String ...

  10. jeecg Export导出图片到excel

    import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; impo ...