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 (ie 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, eg 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

就是sg函数的运用,知识变成多组例子而已,记住一点,不能取的点就是0

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(a,b) memset((a),(b),sizeof(a))
#include<vector>
typedef long long ll;
typedef double db;
const ll mod=1e9+7;
using namespace std;
const double pi=acos(-1.0);
int a[105],sg[10005],d[105],heap[10005],cas;
int getsg(int n)
{
mm(d,0);
int k=0;
for(int i=0;i<cas;i++)
{
if(a[i]>n)
break;
if(n>=a[i])
{
d[k++]=sg[n-a[i]];
}
}
if(k==0) return 0;
d[k]=mod;
sort(d,d+k);
if(d[0]!=0) return 0;
for(int i=0;i<k;i++)
if(d[i+1]-d[i]>1)
return d[i]+1;
}
void first(int n)
{
mm(sg,0);
sg[0]=0;
for(int i=1;i<=10000;i++)
{
sg[i]=getsg(i);
}
}
int main()
{
int n,num;
while(1)
{
sf("%d",&cas);
if(!cas) return 0;
for(int i=0;i<cas;i++)
sf("%d",&a[i]);
sort(a,a+cas);
first(cas);
sf("%d",&n);
for(int i=0;i<n;i++)
{
sf("%d",&num);
int x=0;
for(int j=0;j<num;j++)
{
sf("%d",&heap[j]);
x^=sg[heap[j]];
}
if(x)
pf("W");
else
pf("L");
}
pf("\n");
} }

J - S-Nim的更多相关文章

  1. bzoj 2281 [Sdoi2011]黑白棋(博弈+组合计数)

    黑白棋(game) [问题描述] 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色 ...

  2. jzoj5804

    這道題n-m很小,可以從此入手 記f[i][j]為i個字符括號綜合為j的合法方案數 則第i個括號可以枚舉為(和),所以f[i][j]=f[i-1][j-1]+f[i-1][j+1],小心越界 再記a為 ...

  3. NOIP前的模板

    1.筛\(phi\) \(logn\)求少数\(phi\) inline int phi(R int x){ R int res=x,tmp=x; for(R int i=2;i*i<=x;i+ ...

  4. HDU 1847 博弈

    sg[0]=0; sg[i]=mex{sg[i-2^(j)]}  (i>=2^j) mex()为不在此集合的最小非负整数 #include <stdio.h> #include &l ...

  5. ACM模板_axiomofchoice

    目录 语法 c++ java 动态规划 多重背包 最长不下降子序列 计算几何 向量(结构体) 平面集合基本操作 二维凸包 旋转卡壳 最大空矩形 | 扫描法 平面最近点对 | 分治 最小圆覆盖 | 随机 ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  8. 【SRM】518 Nim

    题意 \(K(1 \le K \le 10^9)\)堆石子,每堆石子个数不超过\(L(2 \le 50000)\),问Nim游戏中先手必败局面的数量,答案对\(10^9+7\)取模. 分析 容易得到\ ...

  9. BZOJ 3105 [CQOI2013]新Nim游戏 ——线性基

    [题目分析] 神奇的题目,两人都可以第一次取走足够多堆的石子. nim游戏的规则是,如果异或和为0,那么就先手必输,否则先手有必胜策略. 所以只需要剩下一群异或和为0就可以了. 先排序,线性基扫一遍即 ...

  10. 【BZOJ-2460&3105】元素&新Nim游戏 动态维护线性基 + 贪心

    3105: [cqoi2013]新Nim游戏 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 839  Solved: 490[Submit][Stat ...

随机推荐

  1. 反向代理WebSocket连接自动断掉的问题

    Nginx可能设置了超时时间,导致WebSocket一会儿就断了 解决方法: 1.增加Nginx配置 proxy_read_timeout 500s; 注:三种超时时间,参见 https://www. ...

  2. VMware DHCP Service服务无法启动问题的解决

    我的电脑出现VMware DHCP Service和VMware NAT Service两个服务无法启动的问题: 打开VMware主界面,菜单->编辑->虚拟网络编辑器: 勾选上“将主机虚 ...

  3. 原创:vsphere概念深入系列五:存储

    1.vSphere支持的存储文件格式: 类似于linux下挂载文件系统,需要有驱动器设备,驱动. 挂载后有挂载路径. vSphere 也是一样处理. 挂载名:挂载后可以给存储设备起名,默认为datas ...

  4. 【NIO】Java NIO之通道

    一.前言 前面学习了缓冲区的相关知识点,接下来学习通道. 二.通道 2.1 层次结构图 对于通道的类层次结构如下图所示. 其中,Channel是所有类的父类,其定义了通道的基本操作.从 Channel ...

  5. RabbitMQ使用技巧

    一. net客户端介绍    http://www.cnblogs.com/hsyzero/p/6297644.html 二. RabbitMQ消息应答 执行一个任务可能需要花费几秒钟,你可能会担心如 ...

  6. mysql 5.7中的threads

    >desc threads; +---------------------+---------------------+------+-----+---------+-------+ | Fie ...

  7. Nuget 配置文件的位置

    最近在 Visual Studio 中使用 Nuget 时,发现总是连接代理服务器,忘了什么时候配置的了,找了半天没找到配置位置,最后发现在这个地方: %appdata%\NuGet 找到 NuGet ...

  8. GCD使用:让程序在后台较长久的运行(UIBackgroundTaskIdentifier )

        在没有使用GCD时,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是在使用GCD后,app最多有10分钟的时间在后台长久运行.这个时间可以用来做清理本地 ...

  9. Android 网络知识必知必会

    目录: 网络分层 TCP 和 UDP 区别 TCP 三次握手以及为什么需要三次握手 UDP 四次挥手以及为什么需要四次挥手 socket 开发相关 Http 是什么 Https 是什么以及和 HTTP ...

  10. Netflix开源类库archaius(一)概述

    archaius是什么,能做什么? archaius是Netflix公司开源项目之一,基于java的配置管理类库,主要用于多配置存储的动态获取.主要功能是对apache common configur ...