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( ...
随机推荐
- 深入理解CMA【转】
转自:https://www.csdn.net/article/a/2016-07-07/15839383 摘要:连续内存分配(简称CMA) 是一种用于申请大量的,并且物理上连续的内存块的方法,在LW ...
- java并发编程系列七:volatile和sinchronized底层实现原理
一.线程安全 1. 怎样让多线程下的类安全起来 无状态.加锁.让类不可变.栈封闭.安全的发布对象 2. 死锁 2.1 死锁概念及解决死锁的原则 一定发生在多个线程争夺多个资源里的情况下,发生的原因是 ...
- centos6.7环境下kvm虚拟机之virt-install和virsh及virt-manager工具的使用
virt-install工具的使用: virt-install是一个命令行工具,它能够为KVM.Xen或其它支持libvrit API的hypervisor创建虚拟机并完成GuestOS安装:此外,它 ...
- echarts地图使用
在使用echarts3当中,地图需要的数据时分开的,需要自己下载,而在echarts3官网上的地图数据只有全国.中国.各省的地图 如果我们需要使用更详细的地图,需要在echarts2的生成需要的地级市 ...
- androidpn 推送系统
(文中部分内容来自网络,如无意中侵犯了版权,请告之!) XMPP协议: XMPP : The Extensible Messaging andPresence Protocol. 中文全称:可扩展通讯 ...
- Nodejs实现WebSocket通信demo
一.创建websocket.js文件 步骤: 1.创建websocket.js文件,复制如下代码: 2.安装nodejs-websocket依赖: 3.该文件夹下命令行执行 node websocke ...
- PYTHON-文件处理-练习
## 一.实现用户注册功能# 思路:# 用户输入用户名.密码# 将用户输入的内容按照固定的格式,比如:egon:123,存入文件# 可以往一个文件中重复注册新的用户名和密码# 附加:# 1.对输入的用 ...
- CGAffineTransform 缩放 / 旋转 / 平移
CGAffineTransform此类是一个3*3矩阵的变换. - (void)transformImageView { CGAffineTransform t = CGAffineTransform ...
- h5之js生成二维码
目录架构: index.html <!DOCTYPE> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang=& ...
- PHP实现的简单组词算法
?php //组词算法 function diyWords($arr,$m){ $result = array(); if ($m ==1){//只剩一个词时直接返回 return $arr; } i ...