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. POJ:2395-Out of Hay

    Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18780 Accepted: 7414 Descripti ...

  2. CF797E. Array Queries

    a is an array of n positive integers, all of which are not greater than n. You have to process q que ...

  3. 大数据江湖之即席查询与分析(下篇)--手把手教你搭建即席查询与分析Demo

    上篇小弟分享了几个“即席查询与分析”的典型案例,引起了不少共鸣,好多小伙伴迫不及待地追问我们:说好的“手把手教你搭建即席查询与分析Demo”啥时候能出?说到就得做到,差啥不能差人品,本篇只分享技术干货 ...

  4. [bzoj3371][poj2009][Usaco2004 Mar]Moo University - Emergency Pizza Order 定制比萨饼

    标题这么长的..真是让感觉人头大脚轻. 贴题面先. Description     Moo大学的餐厅必须为$C(1\leq C\leq 1000)$头入学的奶牛新生定制比萨饼.比萨饼可以在比萨小屋订做 ...

  5. Spring---bean的实例化

    Spring IoC容器如何实例化Bean呢?传统应用程序可以通过new和反射方式进行实例化Bean.而Spring IoC 容器则需要根据Bean定义里的配置元数据使用反射机制来创建Bean.在Sp ...

  6. Installation error: INSTALL_FAILED_CANCELLED_BY_USER

    我的手机本来是支持Androidstadio 调试手机的,我手机小米的,后来,系统升级了,我也没在意,第二天上班,已运行就报错: Installation error: INSTALL_FAILED_ ...

  7. Android stadio 模板 liveTemplate不管用

    今天自己弄了模板,发现不生效.后来才知道要在下面设置在哪里应用:如下图: Android satdio 制作自己的todo 有时候,别人都使用todo,使得自己个人的todo不好用了.那么怎么弄?自己 ...

  8. DOS程序员手册(三)

    56页     第4章DOS和BIOS接口     本章介绍了用户程序访问DOS内核和BIOS所提供的各种服务的方法.为了访问这 些服务,我们可以从任何编程语言中调用各个软件中断,这些中断便是我们在本 ...

  9. 孤荷凌寒自学python第二十五天初识python的time模块

    孤荷凌寒自学python第二十五天python的time模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 通过对time模块添加引用,就可以使用python的time模块来进行相关的时间操 ...

  10. 1099 Build A Binary Search Tree (30 分)(查找二叉树)

    还是中序遍历建树 #include<bits/stdc++.h> using namespace std; ; struct node { int data; int L,R; }s[N] ...