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.

InputInput consists of a number of test cases. 
For each test case: The rst 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.OutputFor 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博弈,最终答案等于每个NIMA博弈结果的异或;(注意求SG函数时,不要每次都把vis数组清空,用一个t标记即可,每次改变标记,否则会超时)
参考代码:
 #include<bits/stdc++.h>
using namespace std;
#define clr(a,val) memset(a,val,sizeof a)
const int maxn=;
int num,f[maxn],ans;
int l,t,cas,SG[maxn],vis[maxn];
void GetSG(int x)
{
clr(SG,);
int t=;
for(int i=;i<=x;++i)
{
for(int j=;f[j]<=i&&j<=num;++j) vis[SG[i-f[j]]]=t;
for(int j=;j<=x;j++) {if(vis[j]!=t) {SG[i]=j;break;}}
++t;
}
} int main()
{
while(~scanf("%d",&num) && num)
{
for(int i=;i<=num;++i)scanf("%d",&f[i]);
sort(f+,f++num);
GetSG(maxn-);
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&l);ans=;
for(int i=;i<=l;++i) scanf("%d",&t),ans^=SG[t];
if(!ans) printf("L");
else printf("W");
}
puts("");
}
return ;
}

HDU1944 S-NIM(多个NIM博弈)的更多相关文章

  1. NIM游戏,NIM游戏变形,威佐夫博弈以及巴什博奕总结

    NIM游戏,NIM游戏变形,威佐夫博弈以及巴什博奕总结 经典NIM游戏: 一共有N堆石子,编号1..n,第i堆中有个a[i]个石子. 每一次操作Alice和Bob可以从任意一堆石子中取出任意数量的石子 ...

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

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

  3. HDU 3032 Nim or not Nim?(博弈,SG打表找规律)

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. HDU 3032 Nim or not Nim? (sg函数)

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. HDU 5795 A Simple Nim(简单Nim)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. HDU 3032 Nim or not Nim? (sg函数求解)

    Nim or not Nim? Problem Description Nim is a two-player mathematic game of strategy in which players ...

  7. Nim or not Nim? hdu3032 SG值打表找规律

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. 【HDU3032】Nim or not Nim?(博弈论)

    [HDU3032]Nim or not Nim?(博弈论) 题面 HDU 题解 \(Multi-SG\)模板题 #include<iostream> #include<cstdio& ...

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

  10. HDU 3032 Nim or not Nim?(Multi_SG,打表找规律)

    Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. 基于docker搭建Jenkins+Gitlab+Harbor+Rancher架构实现CI/CD操作(续)---Harbor的安装

    前期安装文档:https://www.cnblogs.com/lq-93/p/11828626.html Harbor的作用:     开发提交代码至gitlab容器中,Jenkins拉取代码构建镜像 ...

  2. win7设置docker默认服务端地址

    目录 win7设置docker默认服务端地址 1.开启docker远程访问 2.本地调整 2.1 docker.exe重命名 2.2 添加docker.bat 2.3 添加快速切换功能 3.使用验证 ...

  3. Python 基础 内置函数 迭代器与生成器

    今天就来介绍一下内置函数和迭代器 .生成器相关的知识 一.内置函数:就是Python为我们提供的直接可以使用的函数. 简单介绍几个自己认为比较重要的 1.#1.eval函数:(可以把文件中每行中的数据 ...

  4. nyoj 975-关于521 (EOF)

    975-关于521 内存限制:64MB 时间限制:1000ms 特判: No 通过数:5 提交数:46 难度:2 题目描述: Acm队的流年对数学的研究不是很透彻,但是固执的他还是想一头扎进去. 浏览 ...

  5. 安卓手机运行fedora

    安卓手机使用容器运行其他linux,一般两种: 1. termux + rootfs.img + proot,依赖api>=21,不必root但受限. 2. linuxdeploy + proo ...

  6. vue引用组件的两个方法

    <template> <div> <myComponent></myComponent> </div> </template> ...

  7. pycharm设置python脚本模板

    PyCharm PyCharm是一个有名的Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成 ...

  8. 剑指Offer-25.复杂链表的复制(C++/Java)

    题目: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head.(注意,输出结果中请不要返回参数中的节点引用,否则 ...

  9. 新闻实时分析系统 Spark2.X环境准备、编译部署及运行

    1.Spark概述 Spark 是一个用来实现快速而通用的集群计算的平台. 在速度方面, Spark 扩展了广泛使用的 MapReduce 计算模型,而且高效地支持更多计算模式,包括交互式查询和流处理 ...

  10. 初探three.js材质

    这节我们浅谈一下THREE的材质.材质就是物体的皮肤,决定物体的表面.THREE的材质有很多种,他们有的和到相机的距离有关,有的和面的法向量角度有关,有的不受光照的影响,有的受到光照的影响会产生反射效 ...