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;
}
HDU.1536.S-Nim(博弈论 Nim)的更多相关文章
- HDU 3404 Switch lights 博弈论 nim积
http://acm.hdu.edu.cn/showproblem.php?pid=3404 题目 http://www.doc88.com/p-5098170314707.html 论文 nim积在 ...
- HDU 3094 树上删边 NIM变形
基本的树上删边游戏 写过很多遍了 /** @Date : 2017-10-13 18:19:37 * @FileName: HDU 3094 树上删边 NIM变形.cpp * @Platform: W ...
- POJ 3553 Light Switching Game 博弈论 nim积 sg函数
http://poj.org/problem?id=3533 变成三维的nim积..前面hdu那个算二维nim积的题的函数都不用改,多nim积一次就过了...longlong似乎不必要但是还是加上了 ...
- (转载)Nim博弈论
最近补上次参加2019西安邀请赛的题,其中的E题出现了Nim博弈论,今天打算好好看看Nim博弈论,在网上看到这篇总结得超级好的博客,就转载了过来. 转载:https://www.cnblogs.com ...
- S-Nim HDU 1536 博弈 sg函数
S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...
- HDU.2149 Public Sale (博弈论 巴什博弈)
HDU.2149 Public Sale (博弈论 巴什博弈) 题意分析 巴什博奕裸题 博弈论快速入门 代码总览 #include <bits/stdc++.h> using namesp ...
- HDU.1846 Brave Game (博弈论 巴什博弈)
HDU.1846 Brave Game (博弈论 巴什博弈) 题意分析 巴什博奕裸题 博弈论快速入门 代码总览 include <bits/stdc++.h> using namespac ...
- 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 ...
- 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( ...
随机推荐
- php 日期格式转换万能公式
思路用strtotime转换时间的字符串 $t='2017-03-09 02:30'; echo(date('Y-m-d H-i', strtotime($t)));
- javascrip学习之 数据类型和变量
JavaScript 是脚本语言.是一种轻量级的编程语言.是可插入 HTML 页面的编程代码,可由所有的现代浏览器执行. JavaScript的语法和Java语言类似,每个语句以;结束,语句块用{.. ...
- jquery submit ie6下失效的原因分析及解决方法
ie6中, $('a.btn').click(function(){ form.submit(); }) 点击失效: 分析: 微软低版本浏览器会先执行link标签的自身事件也就是href事件,这样就中 ...
- linux 后台运行nohup & ctrl+z
使用Linux时,经常希望有些命令结果不在前台显示,如sh脚本,耗时的命令等.一般情况下,使用 & 将命令结果后台运行,如sh test.sh,脚本后台执行. 有时候命令已经在前台执行了,需要 ...
- 【python】xsspider零碎知识点
1.提取url信息 urlparse() from urlparse import urlparse url = "http://scrapy-chs.readthedocs.io/zh_C ...
- python 全栈开发,Day99(作业讲解,DRF版本,DRF分页,DRF序列化进阶)
昨日内容回顾 1. 为什么要做前后端分离? - 前后端交给不同的人来编写,职责划分明确. - API (IOS,安卓,PC,微信小程序...) - vue.js等框架编写前端时,会比之前写jQuery ...
- 《剑指offer》-找到字符串中第一个只出现一个的字符
题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- 【C++ Primer 第11章 练习答案】2. 关联容器概述
11.2.1节练习 [练习11.7]代码: #include<iostream> #include<string> #include<vector> #includ ...
- 如何批量的在django中对url进行用户登陆限制
参考URL: https://blog.csdn.net/hanshengzhao/article/details/79540306?utm_source=blogxgwz0 1,首先定义一个内部有装 ...
- Hibernate的CRUD以及junit测试
Hibernate的CRUD以及junit测试 1:第一步创建动态工程引包,省略. 2:第二步,创建数据库和数据表,省略. 3:第三步,创建实体类,如User.java,源码如下所示: 对于实体类,一 ...