http://poj.org/problem?id=2960

S-Nim
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3464   Accepted: 1829

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

Source

 
题意:给定数组S,接下来给出m个游戏局面。游戏局面是一些beads堆,先给出堆数,然后是每一堆中beads的数目。游戏规则是,两个人轮流取beads,每次可以选择一堆,从中取出k个beads,k ∈S,最后不能取的人输。 
 
新学的博弈,记下来,先介绍Nim 游戏
情况1 两堆石头,a,b每次可以从任意一堆中拿去任意个数个石头,最后不拿的输  ,要是有两堆石子,要是这两堆石子数目一样的话,先走的肯定会输,因为只要a先拿的话,b可以重复a上面的操作,和a在不同的堆中拿相同的个数,两个数相等的时候其异或值相等,及^想等,
而要是两堆石子的个数不同的话,先拿的可以通过第一次拿走其中个数较多的那一堆石头的一部分,使得剩余的两堆石头个数相同
情况2 n 堆石头,每堆石头有xi个石头,a和b 每次可以选取其中一堆拿取任意数目的石头,最后不能拿的人输。 设必胜态为1, 必败态为 设有某状态后面有n个后续状态,(后继状态就是a拿完后,b应该拿的时候的状态)要是所有的后继状态中有一个是0的话这个状态就是1,而要是后继状态中所有都是1 ,那么这个状态就是0,一步可以走到必败态的一定是必胜态,现在给出一个结论,将每堆石头的数目异或起来,如果等于0的话是必败态,要是不等于0为必胜态,现在假设三堆得数目是a b c , 假设a^b^c=k ;(k!=0)  那么k的第一位上的1肯定是来自于a b c 中的某一个,假设来自b ,那么b^k<b 有第一次拿b-(b^k)个石头,那么,下一个状态的异或值就是a^(b^k)^c =k^k =0 ,依次递归下去,最后三堆石头数目都是0的时候肯定是必败态,所以状态为0的肯定是必败态,而异或值不为0的可以通过一次取石头达到必败态,所以是必胜态。
 
下面介绍sg函数: 定义:为集合中没有出现过的最小的非负整数,s = {1,2,3,4,5}  则sg(s) = 0 ;  s ={0,1,2,3}  sg(s)=4 ;
 情况1: 有一堆石头 K 个,每次只能拿x个,x属于s = {2,4,5} 。做法,要寻找前驱,
集合k是k的所有前驱的sg值得集合 例: sg(8) = sg({sg(6),sg(4),sg(3)};
做法,求出sg(k)即可,要是sg(k) = 0 则为必败态,sg(1) !=0 为必胜态,递归的求 sg(0)  = 0 ;因为没有前驱,sg(1) = 0 ; sg(2) = sg{0} = 1;
sg(3) = sg({sg(1)}) = 1 ; sg(4) = sg({sg(2),sg(0)}) = 2;sg(5) = sg({sg(3),sg(1), sg(0)} = 2;
 
情况2 : 有n 堆石头,每堆xi个石头,每次从第i 堆中只能取si = {……}中的值得个数,最后不能拿的人输
做法: 求出每一堆得sg(xi);假设sg(xi) = k ;则它的后驱一定有k-1,k-2,......这样的话,就相当是一个Nim 游戏了,每堆都有k个石头都必须取,可以取任意多个石头,
所以将每一个sg(xi)异或起来,然后要是结果为0则必败
 
提示:一般博弈题,就把所给值异或起来一般就可以做了
 
现在给出Nim游戏用sg函数的证明, nim 游戏相当于是每次取得个数都是123……n,所以sg(0) = 0 ; sg(n) = n ;
 
 
下面是代码:
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 105
#define M 10005 int s[N], sn;
int sg[M]; void getsg(int n)
{
int mk[M];
sg[] = ;//主要是让终止状态的sg为0
memset(mk, -, sizeof(mk));
for(int i = ; i < M; i++)//预处理sg函数
{
for(int j = ; j < n && s[j] <= i; j++)
mk[sg[i-s[j]]]=i;//将所有后继的sg标记为i,然后找到后继的sg没有出现过的最小正整数
//优化:注意这儿是标记成了i,刚开始标记成了1,这样每次需初始化mk memset,而标记成i就不需要了
int j = ;
while(mk[j] == i) j++;
sg[i] = j;
}
} int main()
{
while(~scanf("%d", &sn), sn)
{
for(int i = ; i < sn; i++) scanf("%d", &s[i]);
sort(s, s+sn);//排序算一个优化,求sg的时候会用到
getsg(sn);
int m;
scanf("%d", &m);
char ans[N];
for(int c = ; c < m; c++)
{
int n, tm;
scanf("%d", &n);
int res = ;
for(int i = ; i < n; i++)
{
scanf("%d", &tm);
res ^= sg[tm];
}
if(res == ) ans[c] = 'L';
else ans[c] = 'W';
}
ans[m]=;
printf("%s\n", ans);
}
return ;
}
 

SG 函数 S-Nim的更多相关文章

  1. sg函数和nim游戏的关系

    sg函数和nim游戏的关系 本人萌新,文章如有错漏请多多指教-- 我在前面发了关于nim游戏的内容,也就是说给n堆个数不同的石子,每次在某个堆中取任意个数石子,不能取了就输了.问你先手是否必胜.然后只 ...

  2. 博弈论基础之sg函数与nim

    在算法竞赛中,博弈论题目往往是以icg.通俗的说就是两人交替操作,每步都各自合法,合法性与选手无关,只与游戏有关.往往我们需要求解在某一个游戏或几个游戏中的某个状态下,先手或后手谁会胜利的问题.就比如 ...

  3. 【UVA11859】Division Game(SG函数,Nim游戏)

    题意:给定一个n*m的矩阵,两个游戏者轮流操作. 每次可以选一行中的1个或多个大于1的整数,把它们中的每个数都变成它的某个真因子,不能操作的输. 问先手能否获胜 n,m<=50,2<=a[ ...

  4. Nowcoder 挑战赛23 B 游戏 ( NIM博弈、SG函数打表 )

    题目链接 题意 : 中文题.点链接 分析 : 前置技能是 SG 函数.NIM博弈变形 每次可取石子是约数的情况下.那么就要打出 SG 函数 才可以去通过异或操作判断一个局面的胜负 打 SG 函数的时候 ...

  5. SG函数和SG定理【详解】

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

  6. SG函数&&SG定理

    必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点,处于此情况下,双方操作均正确的情况下必胜. 必胜点和必败点的 ...

  7. sg函数总结

    http://blog.csdn.net/luomingjun12315/article/details/45555495 这一段时间写的题和我接下来要展示的一些概念都来自这里↑. 必胜点和必败点的概 ...

  8. (转载)--SG函数和SG定理【详解】

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

  9. SG函数略解

    由于笔者太懒,懒得把原来的markdown改成MCE,所以有很多奇怪的地方请谅解. 先说nim游戏. 大意:有n堆石子,两个人轮流取,每个人每次从任意一堆取任意个,直到一个人无法取了为止.问对于石子的 ...

  10. 组合游戏 - SG函数和SG定理

    在介绍SG函数和SG定理之前我们先介绍介绍必胜点与必败点吧. 必胜点和必败点的概念:        P点:必败点,换而言之,就是谁处于此位置,则在双方操作正确的情况下必败.        N点:必胜点 ...

随机推荐

  1. Wincc flexable的画面浏览切换组态

    1.新建项目和6个画面 2.双击导航控件设置,选择默认设置 3.使用画面浏览编辑器编辑画面层次切换关系,拖拽画面到编辑器中进行关系连接 4.保运并运行

  2. Linux 学习记录 三(Vim 文书编辑器).

            所有的Unix Like系统都会内建vi文书编辑器,其他的文书编辑器不一定存在,vim是vi的升级版,具有程序编辑的能力,可以主动的以字体颜色辨别语法的正确性,方便程序设计.vim 里 ...

  3. Hibernate缓存和状态

    缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.   缓存的介质一般是内存,所以读写速度很快.但如果缓存中存放的数据量非常大时,也会用硬盘 ...

  4. 在Ubuntu14.04下安装 ffmpeg-2.4.13(处理视频用,将视频保存为图片序列)

    首先在 http://www.ffmpeg.org/olddownload.html 下载 ffmpeg-2.4.13.tar.bz2 : 然后安装 yasm 和 libx264: apt-get i ...

  5. KVM(二):KVM应用

    ++++++++++++++++++++++++++++++创建和拍摄快照++++++++++++++++++++++++++++++++++ KVM快照方法常用的是qemu-img snapshot ...

  6. AngularJS 模板

    一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模式解耦代码和分离关注点.考虑到这一点,我们用AngularJS来为我们的应用添加一些模型.视图和控制器 ...

  7. HTTPS加密流程超详解(二)

    2.进入正题 上篇文章介绍了如何简单搭建一个环境帮助我们分析,今天我们就进入正题,开始在这个环境下分析. 我们使用IE浏览器访问Web服务器根目录的test.txt文件并抓包,可以抓到如下6个包(前面 ...

  8. JSP EL隐含对象

    JSP 内置对象 JSP EL隐含对象 描述 page pageScope page 作用域 request requestScope request 作用域 session sessionScope ...

  9. docker for windows & dotnet core app

    Step 1: 安装docker for windows Step 2: 从github 上 clone 源代码:https://github.com/dotnet/dotnet-docker-sam ...

  10. CSS Sprites使用

    CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢 ...