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 ...
随机推荐
- Dev 日志 | 一次 Segmentation Fault 和 GCC Illegal Instruction 编译问题排查 NebulaGraph
摘要 笔者最近在重新整理和编译 Nebula Graph 的第三方依赖,选出两个比较有意思的问题给大家分享一下. Flex Segmentation Fault--Segmentation fault ...
- ReactJS中的自定义组件
可控自定义组件: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- docker中部署项目时遇到的问题
容器和宿主机时间不同步问题? 将本地时间复制到docker容器内的etc文件夹下即可 docker cp /etc/localtime scrapy_8:/etc/ 启动crontab错误? 报错: ...
- 小白学习React官方文档看不懂怎么办?2.JSX语法
接下来我们就要讲到JSX语法了,在我们讲它之前,我们先引入一个概念叫语法糖. 听到这个名字首先我们可能会想到一个词叫”糖衣炮弹“,那么什么叫糖衣炮弹呢,就是给你说各种好听的话,来迷惑你,但 ...
- PHP提高SESSION响应速度的方法有哪些
1.设置多级目录存储SESSION 默认session的存储目录是1级目录,如果用户量比较大,session文件数量就比较大,我们可以设置目录数为2,使用2级目录可以提交查找和存取速度.不过这种方式对 ...
- 2019-11-26:密码学基础知识,csrf防御
信息安全的基础是数学--->密码算法--->安全协议(ssl VPN)-->应用(证书 PKI)密码学入门密码编码学:研究加解密算法的学科密码分析学:研究破译密码算法的学科 加解密分 ...
- Spring与Shiro整合 登陆操作
Spring与Shiro整合 登陆操作 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 编写登陆Controller方法 讲解: 首先,如果你登陆失败的时候,它会把你的异常信息丢到 ...
- win10 下安装zookeeper+Kafka 的详细步骤(2)
Win10 下要使用Kafka需要经过以下三个步骤: 1.安装JDK(需要安装依赖java JDK) 2.安装zookeeper(资源协调,分配管理) 3.使用Kafka开发 ============ ...
- 环境配置——tornado项目准备工作
新建tornado项目后,采用Pycharm作为开发工具,采用Xshell链接Ubuntu模拟服务端方便方便测试.项目编码前进行以下几个方面的配置. 1.Ubuntu配置 1.1安装ssh服务 sud ...
- length of the longest substring without repeating character
Given a string, find the length of the longest substring without repeating characters. 来源:力扣(LeetCod ...