HDU1536 S-Nim
S-Nim
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7638 Accepted Submission(s): 3215
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.
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
WWL
题意:首先输入K 表示一个集合的大小 之后输入集合 表示对于这对石子只能去这个集合中的元素的个数
之后输入 一个m 表示接下来对于这个集合要进行m次询问
之后m行 每行输入一个n 表示有n个堆 每堆有n1个石子 问这一行所表示的状态是赢还是输 如果赢输入W否则L
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=;
int n,m,cas,State,a[N],f[N],SG[N];bool mex[N];
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void GetSG(int n){
memset(SG,,n+<<);
for(int i=;i<=n;i++){
memset(mex,,n+);
for(int j=;f[j]<=i;j++) mex[SG[i-f[j]]]=;
for(int j=;j<=n;j++) if(!mex[j]){SG[i]=j;break;}
}
}
int main(){
while(){
n=read();
if(!n) break;
for(int i=;i<=n;i++) f[i]=read();
sort(f+,f+n+);f[n+]=2e9;
GetSG();
cas=read();
while(cas--){
State=;m=read();
for(int i=;i<=m;i++) a[i]=read();
for(int i=;i<=m;i++) State^=SG[a[i]];
putchar(State?'W':'L');
}
putchar('\n');
}
return ;
}
HDU1536 S-Nim的更多相关文章
- hdu1536&&hdu3023 SG函数模板及其运用
S-Nim Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status ...
- [LeetCode] Nim Game 尼姆游戏
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- LeetCode 292. Nim Game
Problem: You are playing the following Nim Game with your friend: There to stones. The one who remov ...
- 【SRM】518 Nim
题意 \(K(1 \le K \le 10^9)\)堆石子,每堆石子个数不超过\(L(2 \le 50000)\),问Nim游戏中先手必败局面的数量,答案对\(10^9+7\)取模. 分析 容易得到\ ...
- HDU 2509 Nim博弈变形
1.HDU 2509 2.题意:n堆苹果,两个人轮流,每次从一堆中取连续的多个,至少取一个,最后取光者败. 3.总结:Nim博弈的变形,还是不知道怎么分析,,,,看了大牛的博客. 传送门 首先给出结 ...
- HDU 1907 Nim博弈变形
1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形. ...
- Nim游戏
目前有3堆石子,每堆石子个数也是任意的,双方轮流从中取出石子,规则如下:1)每一步应取走至少一枚石子:每一步只能从某一堆中取走部分或全部石子:2)如果谁不能取谁就失败. Bouton定理: 必败状态当 ...
- HDU 3032 Nim or not Nim (sg函数)
加强版的NIM游戏,多了一个操作,可以将一堆石子分成两堆非空的. 数据范围太大,打出sg表后找规律. # include <cstdio> # include <cstring> ...
随机推荐
- MongoDB,还有一个角度看数据
传智-玄痛(传智播客北京校区C/C++学院技术指导老师) MongoDB的起源 几年前 10gen 公司做了 SaaS 方面的研发,由于公司一个 MongoDB 产品存储接口的易用性,用户评价很好,公 ...
- vim资源帖
vimscript教程 http://learnvimscriptthehardway.stevelosh.com/ 阿信的vimscript http://www.axiaoxin.com/arti ...
- ECN
ECN是工程变更管理,主要用来管理BOM的生效日期,及记录BOM的更改内容:在SAP系统中,需先创建ECN更改号码,凭ECN号码去更改管理BOM
- queue for max elem, pop, push
queue for max elem, pop, push 个人信息:就读于燕大本科软件project专业 眼下大三; 本人博客:google搜索"cqs_2012"就可以; 个人 ...
- vim -d file01 file02 diff file01 file02 对比两文件的不同
[root@86 vhosts]# vim -d defaul.conf.bak zabbix.xinxianm.com.conf server { | server { listen 80; | l ...
- 03、矢量图形查询工具(Symbol Unicode)
目前的软件开发中,很多地方都使用到了矢量图标,在 Metro app 的开发中,可以使用 Windows 系统图标(02.Universal app 中按钮图标使用 ),包括 Segoe UI Sym ...
- oracle锁表,杀死进程
查询锁表数据 select object_name,machine,s.sid,s.serial#from v$locked_object l,dba_objects o ,v$session swh ...
- phan—php语法静态检查在windows下的配置
php7推出了语法树,于是静态语法分析更方便了.(可以直接下载我配置好的,传送门百度网盘) 1.先安装php7,到http://windows.php.net/下载php7带openssl的. 2.修 ...
- jquery 排除重复
应用场景——双盒选择器 两个select可能会出现重复的情况 排除重复代码如下: /** * 删除$fromGroup中与$toGroup重复的option * @param $fromGroup = ...
- string--C++ STL 学习
C++STL提供了string基本字符系列容器来处理字符串,可以把string理解为字符串类,提供了添加删除\替换\查找和比较等丰富的方法. 使用string容器,需要包含头文件声明#include ...