Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows:

  • 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.

Input

Input consists of a number of test cases. 
For each test case: The first 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.

Output

For 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游戏题目中已经给出了——每一堆选取的数量没有限制。

S-Nim游戏仅仅是限制了每一次从每一堆中选取的个数,依旧用sg函数计算即可。

经典的Nim游戏中sg(x) = x,所以结果就是每一堆的状态直接xor即可,S-Nim游戏先计算每一堆的sg函数值,然后判断方法依旧是用xor。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 102
int n,m,l,s[N],sg[],ans,a;
bool vis[];
void c()
{
sg[]=;
for(int i=;i<=;i++)
{
memset(vis,,sizeof(vis));
for(int j=;j<n&&s[j]<=i;j++)
vis[sg[i-s[j]]]=;
for(int x=;x<=;x++)
if(!vis[x])
{
sg[i]=x;
break;
}
}
}
int main()
{
while(cin>>n&&n)
{
for(int i=;i<n;i++)
cin>>s[i];
sort(s,s+n);
c();
cin>>m;
while(m--)
{
ans=;
cin>>l;
while(l--)
{
cin>>a;
ans^=sg[a];
}
if(ans)
cout<<"W";
else
cout<<"L";
}
cout<<endl;
}
return ;
}

对sg函数的理解:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 102
int n,m,l,s[N],sg[],ans,a;
bool vis[];
void c()
{
sg[]=;
for(int i=;i<=;i++)
{
memset(vis,,sizeof(vis));
for(int j=;j<n&&s[j]<=i;j++)
vis[sg[i-s[j]]]=; //里面的sg[]是sg[i]的后继 vis[sg[]] 指的是求mex
for(int x=;x<=;x++)
if(!vis[x])
{
sg[i]=x;
break;
}
}
}
int main()
{
while(cin>>n&&n)
{
for(int i=;i<n;i++)
cin>>s[i];
sort(s,s+n);
c();
for(int i=;i<;i++)
cout<<"i: "<<i<<" sg[i]: "<<sg[i]<<endl;
/*cin>>m;
while(m--)
{
ans=0;
cin>>l;
while(l--)
{
cin>>a;
ans^=sg[a];
}
if(ans)
cout<<"W";
else
cout<<"L";
}
cout<<endl;*/
}
return ;
}

让我们再来考虑一下顶点的SG值的意义。当g(x)=k时,表明对于任意一个0<=i<k,都存在x的一个后继y满足g(y)=i。

也就是说,当某枚棋子的SG值是k时,我们可以把它变成0、变成1、……、变成k-1,但绝对不能保持k不变。

不知道你能不能根据这个联想到Nim游戏,Nim 游戏的规则就是:每次选择一堆数量为k的石子,

可以把它变成0、变成1、……、变成k-1,但绝对不能保持k不变。这表明,如果将n枚棋子所在的顶点的 SG值看作n

堆相应数量的石子,那么这个Nim游戏的每个必胜策略都对应于原来这n枚棋子的必胜策略!

HDU1536&&POJ2960 S-Nim(SG函数博弈)的更多相关文章

  1. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  2. 博弈问题之SG函数博弈小结

    SG函数: 给定一个有向无环图和一个起始顶点上的一枚棋子,两名选手交替的将这枚棋子沿有向边进行移动,无法移 动者判负.事实上,这个游戏可以认为是所有Impartial Combinatorial Ga ...

  3. HDU1536:S-Nim(sg函数)

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. 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 ...

  5. SG函数博弈——poj2311

    关于SG函数的博弈 首先定义必败态 x : SG[x]=0 设任意一个状态y,到所有y能到达的状态连一条边,令这些后继为z y : SG[y]=mex(SG[z]) SG[y]==0 : y就是必败态 ...

  6. 多校6 1003 HDU5795 A Simple Nim (sg函数)

    思路:直接打表找sg函数的值,找规律,没有什么技巧 还想了很久的,把数当二进制看,再类讨二进制中1的个数是必胜或者必败状态.... 打表: // #pragma comment(linker, &qu ...

  7. HDU 3032 Nim or not Nim (sg函数)

    加强版的NIM游戏,多了一个操作,可以将一堆石子分成两堆非空的. 数据范围太大,打出sg表后找规律. # include <cstdio> # include <cstring> ...

  8. HDU 5724 Chess (状态压缩sg函数博弈) 2016杭电多校联合第一场

    题目:传送门. 题意:有n行,每行最多20个棋子,对于一个棋子来说,如果他右面没有棋子,可以移动到他右面:如果有棋子,就跳过这些棋子移动到后面的空格,不能移动的人输. 题解:状态压缩博弈,对于一行2^ ...

  9. HDU 1729 Stone Game 石头游戏 (Nim, sg函数)

    题意: 有n个盒子,每个盒子可以放一定量的石头,盒子中可能已经有了部分石头.假设石头无限,每次可以往任意一个盒子中放石头,可以加的数量不得超过该盒中已有石头数量的平方k^2,即至少放1个,至多放k^2 ...

随机推荐

  1. jupyter notebook中出现ValueError: signal only works in main thread 报错 即 长时间in[*] 解决办法

    我在jupyter notebook中新建了一个基于py3.6的kernel用来进行tensorflow学习 但是在jupyter notebook中建立该kernel时,右上角总是显示 服务正在启动 ...

  2. C# 中的正则简单例子

    public static void Main() { Regex rgx = new Regex(@"[S|s]et-[C|c]ookie: (?<cookieName>\w+ ...

  3. svn TortoiseSVN 回滚版本

    原文链接: http://keenwon.com/1072.html SVN是一个版本管理工具,在工作中经常使用,尤其是多人合作开发的时候,版本管理显得更加重要.需要使用回退的场景往往都比较" ...

  4. Eclipse 创建 Java 包---Eclipse教程第09课

    打开新建 Java 包向导 你可以使用新建 Java 包向导来创建 Java 包.Java 包向导打开方式有: 通过点击 "File" 菜单并选择 New > Package ...

  5. php导出数据为CSV文件DEMO

    代码示例: private function _download_send_headers($filename) { // disable caching $now = gmdate("D, ...

  6. MySQL、MongoDB、Redis 数据库之间的区别与使用(本章迭代更新)

    MySQL.MongoDB.Redis 数据库之间的区别与使用 MySQL.MongoDB.Redis 数据库之间的区别与使用(本章迭代更新) update:2019年2月20日 15:21:19(本 ...

  7. 新建git仓库--留

    1.git config 配置配置息,查看配置信息

  8. about 2018

    2018想要完成的10件事情 1 活出更纯粹的自己.                                                      未完成2 自考本科一定要过.       ...

  9. Linux认知之旅【04 进一步了解目录】!

    一.目录是什么? 二.不得不提的文件系统! 三.绝对路经,相对路径

  10. (原)Unreal渲染模块 管线 - 着色器(1)

    @author: 白袍小道 转载悄悄说明下 随缘查看,施主开心就好 说明: 本篇继续Unreal搬山部分的渲染模块的Shader部分, 主要牵扯模块RenderCore, ShaderCore, RH ...