S-Nim
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4113   Accepted: 2158

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
/*
poj 2960 S-Nim 先给你一个集合,然后是类似于NIM游戏,但是你每次只能从这些石碓中取出集合中的个数,
搞出SG值然后进行计算即可
而且这题 sort什么的会RE 囧. hhh-2016-08-02 18:16:21
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <functional>
typedef long long ll;
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
const int maxn = +; int sg[maxn];
int s[maxn];
int n; void SG(int now)
{
if(sg[now] != -)
return ;
int vis[maxn];
memset(vis,,sizeof(vis));
for(int i = ; i < n; i++)
{
int t = now-s[i];
if(t < )
continue;
SG(t);
vis[sg[t]] = ;
} for(int i = ;; i++)
{
if(!vis[i])
{
sg[now] = i;
break;
}
}
} int main()
{
int x,m;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n) != EOF && n)
{
for(int i = ; i < n; i++)
scanf("%d",&s[i]);
//sort(s,s+n);
memset(sg,-,sizeof(sg));
sg[] = ;
scanf("%d",&m);
while(m--)
{
int ans = ,cnt;
scanf("%d",&cnt);
for(int i = ; i < cnt; i++)
{
scanf("%d",&x);
if(sg[x] == -)
SG(x);
ans ^= sg[x];
}
if(ans)
printf("W");
else
printf("L");
}
printf("\n"); }
return ;
}

poj 2960 S-Nim的更多相关文章

  1. HDU3544 Alice's Game && POJ 2960 S-Nim(SG函数)

    题意: 有一块xi*Yi的矩形巧克力,Alice只允许垂直分割巧克力,Bob只允许水平分割巧克力.具体来说,对于Alice,一块巧克力X i * Y i,只能分解成a * Y i和b * Y i其中a ...

  2. S-Nim POJ - 2960 Nim + SG函数

    Code: #include<cstdio> #include<algorithm> #include<string> #include<cstring> ...

  3. POJ 2960 博弈论

    题目链接: http://poj.org/problem?id=2960 S-Nim Time Limit: 2000MS Memory Limit: 65536K 问题描述 Arthur and h ...

  4. POJ 2960 S-Nim (sg函数)

    题目链接:http://poj.org/problem?id=2960 题目大意:给定数组S,接下来给出m个游戏局面.游戏局面是一些beads堆,先给出堆数,然后是每一堆中beads的数目.游戏规则是 ...

  5. POJ 2960 S-Nim<博弈>

    链接:http://poj.org/problem?id=2960 #include<stdio.h> #include<string.h> ; ; int SG[N];//S ...

  6. POJ 2960 S-Nim 博弈论 sg函数

    http://poj.org/problem?id=2960 sg函数几乎是模板题. 调试代码的最大障碍仍然是手残在循环里打错变量名,是时候换个hydra产的机械臂了[超想要.jpg] #includ ...

  7. poj 2960 S-Nim(SG函数)

    S-Nim Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3694   Accepted: 1936 Description ...

  8. Georgia and Bob POJ - 1704 阶梯Nim

    $ \color{#0066ff}{ 题目描述 }$ Georgia and Bob decide to play a self-invented game. They draw a row of g ...

  9. poj 2960 S-Nim【SG函数】

    预处理出SG函数,然后像普通nim一样做即可 #include<iostream> #include<cstdio> using namespace std; const in ...

随机推荐

  1. PYTHON 词云

    #!/usr/bin/env python # -*- coding:utf-8 -*- import matplotlib.pyplot as plt from wordcloud import W ...

  2. WIN7 局域网共享打印机每次电脑重启后必须登录密码重新连接问题修复

    第一步,WIN+R(或者开始->附件->运行)输入gpedit或gpedit.msc 进入 第二步:把这几个拒绝的Guest给删除掉,也可以只删除""拒绝从王洛访问这台 ...

  3. 自定义SpringBoot启动banner

    序: springboot启动的时候会有一个启动logo似的东西,如图,这个logo似的东西叫做banner,本文小计修改此banner显示与关闭banner.没什么用,有兴趣可以玩玩-- 正文: 自 ...

  4. Python内置函数(13)——bytearray

    英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...

  5. ### Cause: org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg1, arg0, param1, param2]

    org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: org.apache.ib ...

  6. python 字符串的方法

    capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...

  7. tomcat 修改默认字符集

    找到connector节点,插入 disableUploadTimeout="true" useBodyEncodingForURI="true" URIEnc ...

  8. tcpdump记录

    tcpdump -i eth0 -nn -A -X 'host 192.168.20.82 and port 9080' -i:interface 监听的网卡. -nn:表示以ip和port的方式显示 ...

  9. Tcl与Design Compiler (五)——综合库(时序库)和DC的设计对象

    本文如果有错,欢迎留言更正:此外,转载请标明出处 http://www.cnblogs.com/IClearner/  ,作者:IC_learner 前面一直说到综合库/工艺库这些东西,现在就来讲讲讲 ...

  10. Struts(十):OGNL表达式(一)

    Struts2 用s:porperty标签和OGNL表达式来读取值栈中的属性值: I.值栈中的属性值: 1.对象栈:读取对象栈中的某一个对象的属性值: 2.Map栈 :request,session, ...