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枚棋子的必胜策略!

最新文章

  1. flume使用示例
  2. 参考__HTML5 Game
  3. 关于HTML页面布局要注意的问题
  4. MongoDB Map Reduce
  5. 34 个使用 Raspberry Pi 的酷创意
  6. HDU 5365 Run
  7. python 中 struct 用法
  8. pwn-ROP
  9. Nikto学习点
  10. java 三大框架 hibernate部分知识实现增删该查操作
  11. Tesseract_ocr 字符识别基础及训练字库、合并字库
  12. git push 失败
  13. Docker小白从零入门实战
  14. css 元素选择器实例
  15. java script sleep synchronous
  16. ASP.NET MVC4应用程序无法建立控制器的解决方案/获取自己需要的EF版本
  17. cookie函数封装
  18. 使用libpcab抓包&amp;处理包
  19. Friends number NBUT - 1223 (暴力打表)
  20. 求小球反弹高度,及落地过程中经过的路程~~~java代码

热门文章

  1. Java面试宝典2017版
  2. 12 new方法和单例、定制访问函数、装饰器
  3. stdio中牛逼的写法
  4. C# 设定弹出窗体位置
  5. [CodeForces - 296D]Greg and Graph(floyd)
  6. python-9-IO编程
  7. 设计模式--单例模式Singleton
  8. build dynamic libraries for iOS and load them at runtime
  9. 常用doc 命令
  10. android什么时候会产生ANR