HDU1944 S-NIM(多个NIM博弈)
The starting position has a number of heaps, all containing some, not necessarily equal, number of beads.
The players take turns chosing a heap and removing a positive number of beads from it.
The first player not able to make a move, loses.
Arthur and Caroll really enjoyed playing this simple game until they recently learned an easy way to always be able to find the best move:
Xor the number of beads in the heaps in the current position (i.e. if we have 2, 4 and 7 the xor-sum will be 1 as 2 xor 4 xor 7 = 1).
If the xor-sum is 0, too bad, you will lose.
Otherwise, move such that the xor-sum becomes 0. This is always possible.
It is quite easy to convince oneself that this works. Consider these facts:
The player that takes the last bead wins.
After the winning player's last move the xor-sum will be 0.
The xor-sum will change after every move.
Which means that if you make sure that the xor-sum always is 0 when you have made your move, your opponent will never be able to win, and, thus, you will win.
Understandibly it is no fun to play a game when both players know how to play perfectly (ignorance is bliss). Fourtunately, Arthur and Caroll soon came up with a similar game, S-Nim, that seemed to solve this problem. Each player is now only allowed to remove a number of beads in some predefined set S, e.g. if we have S =(2, 5) each player is only allowed to remove 2 or 5 beads. Now it is not always possible to make the xor-sum 0 and, thus, the strategy above is useless. Or is it?
your job is to write a program that determines if a position of S-Nim is a losing or a winning position. A position is a winning position if there is at least one move to a losing position. A position is a losing position if there are no moves to a losing position. This means, as expected, that a position with no legal moves is a losing position.
InputInput consists of a number of test cases.
For each test case: The rst line contains a number k (0 < k <= 100) describing the size of S, followed by k numbers si (0 < si <= 10000) describing S. The second line contains a number m (0 < m <= 100) describing the number of positions to evaluate. The next m lines each contain a number l (0 < l <= 100) describing the number of heaps and l numbers hi (0 <= hi <= 10000) describing the number of beads in the heaps.
The last test case is followed by a 0 on a line of its own.OutputFor each position:
If the described position is a winning position print a 'W'.
If the described position is a losing position print an 'L'.
Print a newline after each test case.Sample Input
2 2 5
3
2 5 12
3 2 4 7
4 2 3 7 12
5 1 2 3 4 5
3
2 5 12
3 2 4 7
4 2 3 7 12
0
Sample Output
LWW
WWL
题解:可以当成多个NIM博弈,最终答案等于每个NIMA博弈结果的异或;(注意求SG函数时,不要每次都把vis数组清空,用一个t标记即可,每次改变标记,否则会超时)
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define clr(a,val) memset(a,val,sizeof a)
const int maxn=;
int num,f[maxn],ans;
int l,t,cas,SG[maxn],vis[maxn];
void GetSG(int x)
{
clr(SG,);
int t=;
for(int i=;i<=x;++i)
{
for(int j=;f[j]<=i&&j<=num;++j) vis[SG[i-f[j]]]=t;
for(int j=;j<=x;j++) {if(vis[j]!=t) {SG[i]=j;break;}}
++t;
}
} int main()
{
while(~scanf("%d",&num) && num)
{
for(int i=;i<=num;++i)scanf("%d",&f[i]);
sort(f+,f++num);
GetSG(maxn-);
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&l);ans=;
for(int i=;i<=l;++i) scanf("%d",&t),ans^=SG[t];
if(!ans) printf("L");
else printf("W");
}
puts("");
}
return ;
}
HDU1944 S-NIM(多个NIM博弈)的更多相关文章
- NIM游戏,NIM游戏变形,威佐夫博弈以及巴什博奕总结
NIM游戏,NIM游戏变形,威佐夫博弈以及巴什博奕总结 经典NIM游戏: 一共有N堆石子,编号1..n,第i堆中有个a[i]个石子. 每一次操作Alice和Bob可以从任意一堆石子中取出任意数量的石子 ...
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- HDU 3032 Nim or not Nim?(博弈,SG打表找规律)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3032 Nim or not Nim? (sg函数)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 5795 A Simple Nim(简单Nim)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 3032 Nim or not Nim? (sg函数求解)
Nim or not Nim? Problem Description Nim is a two-player mathematic game of strategy in which players ...
- Nim or not Nim? hdu3032 SG值打表找规律
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 【HDU3032】Nim or not Nim?(博弈论)
[HDU3032]Nim or not Nim?(博弈论) 题面 HDU 题解 \(Multi-SG\)模板题 #include<iostream> #include<cstdio& ...
- hdu 3032 Nim or not Nim? sg函数 难度:0
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3032 Nim or not Nim?(Multi_SG,打表找规律)
Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- 【Go 入门学习】第一篇关于 Go 的博客--Go 爬虫初体验
一.写在前面 其实早就该写这一篇博客了,为什么一直没有写呢?还不是因为忙不过来(实际上只是因为太懒了).不过好了,现在终于要开始写这一篇博客了.在看这篇博客之前,可能需要你对 Go 这门语言有些基本的 ...
- 银联ISO8583报文解析过程
主密钥: aabbccddeeff11223344556677889900 1.从签到报文中获取工作密钥,包括MACKEY明文,PINKEY明文 签到: 12-03-31 16:38:09----&g ...
- SSM配置梳理
这两天梳理了一下 SSM 的配置,做一个小总结 可能有一些不对的地方,如果您发现了什么错误,非常希望能帮忙指出,谢谢 我参考了很多文章,都标明了来源(链接),可能会影响阅读的连贯性,抱歉 ...
- js在字符串中加入一段字符串
在这个功能的实现主要是slice()方法的掌握 arrayObject.slice(start,end) start 必需.规定从何处开始选取.如果是负数,那么它规定从数组尾部开始算起的位置.也就是说 ...
- C++中对C的扩展学习新增内容———面向对象(封装)
面向对象(封装) 1.对封装的理解: 1.封装就是把变量和函数放在一起统一表示某一个食物. class 2.给类内部的成员增加访问控制权限. 3.封装的语法就是class定义一个类. 2.给对象成员增 ...
- Blob字段
在oracle中有一个特殊的字段类型Blob,Blob是指二进制大对象也就是英文Binary Large Object的缩写: 通过sql取出后,需要进行特殊处理.而我是这样玩的: 1.通过sql查询 ...
- 简单说说基于JWT的token认证方式
一.什么是认证 好多人不知道什么是认证,认证,其实就是服务端确认用户身份.Http协议是无状态的,客户端发送一条请求,服务端返回一条响应,二者就算做成一单买卖,一拍两散.在很久以前,互联网所能提供的服 ...
- AsyncDisplayKit编译和使用注意事项
Facebook开源框架,在github上可下载到.首先要编译AsyncDisplayKit库项目,有可能会出现下面错误: cocoaPods是基于ruby的项目版本控制软件,如果是ruby新手就会不 ...
- [转发]CSR8670的DFU功能
本文源自:https://blog.csdn.net/wzz4420381/article/details/52371409 作者:RyomaWang 申明:为了保持原作者内容,这里不进行任何修改,后 ...
- JSAPI 基于arcgis_js_api3.3的部署
JSAPI,即ArcGIS API For JavaScript,是arcgis基于JavaScript环境下的开发包.包含Dojo框架. ||Dojo结构如下:=================== ...