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( ...
随机推荐
- Python算法:推导、递归和规约
Python算法:推导.递归和规约 注:本节中我给定下面三个重要词汇的中文翻译分别是:Induction(推导).Recursion(递归)和Reduction(规约) 本节主要介绍算法设计的三个核心 ...
- opencv学习笔记(九)Mat 访问图像像素的值
对图像的像素进行访问,可以实现空间增强,反色,大部分图像特效系列都是基于像素操作的.图像容器Mat是一个矩阵的形式,一般情况下是二维的.单通道灰度图一般存放的是<uchar>类型,其数据存 ...
- C++:STL vector:sizeof(vector)
原文地址:http://blog.csdn.net/zcsylj/article/details/7857009 int的大小是4,定义vector<int> vec,vec中有一个元素, ...
- python操作mysql数据库的常用方法使用详解
python操作mysql数据库 1.环境准备: Linux 安装mysql: apt-get install mysql-server 安装python-mysql模块:apt-get instal ...
- centos7忘记登录密码修改
很多时候我们都会忘记Linux root 用户的口令,下面就教大家如果忘记root口令怎么办 第1步:开机后在内核上按“e”.截图如下 按e以后会进入内核启动页面,如下图 第2步:在linux16这行 ...
- selenium和pyquery抓取异步加载数据
安装selenium和pyquery 打开命令行输入: pip install selenium pip install pyquery chromedriver的下载地址如下: http://chr ...
- JDBC辅助类封装 及应用
一:代码图解: 二:配置文件: driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://127.0.0.1\:3306/xlzj_sh_new ...
- linux ubuntu 指令
查找文件:ls -lrt /java 用于查找java文件信息 https://ipcmen.com/ls编辑/etc/profilewen文件,在文件末尾添加export JAVA_HOME=/us ...
- zabbix server+agent+proxy搭建性能监控平台
这是新找到了配置文件配置方法但未尝试 每个模块工作职责: Zabbix Server:负责接收agent发送的报告信息的核心组件,所有配置,统计数据及操作数据均由其组织进行: Database Sto ...
- 线程使用中常见的错误-“System.InvalidOperationException”线程间操作无效: 从不是创建控件“ ”的线程访问它。
“System.InvalidOperationException”类型的未经处理的异常在 System.Windows.Forms.dll 中发生 其他信息: 线程间操作无效: 从不是创建控件“la ...