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. 《Language Implementation Patterns》之 构建语法树

    如果要解释执行或转换一段语言,那么就无法在识别语法规则的同时达到目标,只有那些简单的,比如将wiki markup转换成html的功能,可以通过一遍解析来完成,这种应用叫做 syntax-direct ...

  2. io多路复用(三)

    #!/usr/bin/env python # -*- coding:utf-8 -*- import socket sk1 = socket.socket() sk1.bind(('127.0.0. ...

  3. 【iOS】Swift GCD-上

    尽管Grand Central Dispatch(GCD)已经存在一段时间了,但并非每个人都知道怎么使用它.这是情有可原的,因为并发很棘手,而且GCD本身基于C的API在Swift世界中很刺眼. 在这 ...

  4. (function(root,factory){})(this,function($){}) 一个立即执行的匿名函数自调

    因为新公司用到ocx 我就开始看原来的代码 无意中发现这个 可能原来比较low吗(虽然现在也很low吧)没发现这个东东 还可以这样写 于是乎我开始了探索 完整代码如下 HTML <div id= ...

  5. Django rest framework源码分析(4)----版本

    版本 新建一个工程Myproject和一个app名为api (1)api/models.py from django.db import models class UserInfo(models.Mo ...

  6. LeetCode & Q268-Missing Number-Easy

    Array Math Bit Manipulation Description: Given an array containing n distinct numbers taken from 0, ...

  7. ibatis的优缺点及可行性分析

    1.优点 简单: 易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现. 实用: 提供了数据映射功能,提供了对底层数据访问的封装(例如ado.net),提供了DAO框架,可以使我 ...

  8. 使用 C#/.NET Core 实现单体设计模式

    本文的概念内容来自深入浅出设计模式一书 由于我在给公司做内培, 所以最近天天写设计模式的文章.... 单体模式 Singleton 单体模式的目标就是只创建一个实例. 实际中有很多种对象我们可能只需要 ...

  9. Tumblr:我们是如何从 PHP 5 升级到 PHP 7 的

    Tumblr 团队经常在寻找新的方式来提升网站的性能.这意味着要给访问量大的代码增加缓存,找到更快的 CDN 配置,或者升级基础软件. 最近,通过一次跨团队的努力,我们将全部 web 服务器战舰从 P ...

  10. hdu-1237 简单计算器---中缀表达式转后缀表达式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1237 题目大意: 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. 思路 ...