poj 2960 S-Nim
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 4113 | Accepted: 2158 |
Description
- 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
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
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的更多相关文章
- 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 ...
- S-Nim POJ - 2960 Nim + SG函数
Code: #include<cstdio> #include<algorithm> #include<string> #include<cstring> ...
- POJ 2960 博弈论
题目链接: http://poj.org/problem?id=2960 S-Nim Time Limit: 2000MS Memory Limit: 65536K 问题描述 Arthur and h ...
- POJ 2960 S-Nim (sg函数)
题目链接:http://poj.org/problem?id=2960 题目大意:给定数组S,接下来给出m个游戏局面.游戏局面是一些beads堆,先给出堆数,然后是每一堆中beads的数目.游戏规则是 ...
- POJ 2960 S-Nim<博弈>
链接:http://poj.org/problem?id=2960 #include<stdio.h> #include<string.h> ; ; int SG[N];//S ...
- POJ 2960 S-Nim 博弈论 sg函数
http://poj.org/problem?id=2960 sg函数几乎是模板题. 调试代码的最大障碍仍然是手残在循环里打错变量名,是时候换个hydra产的机械臂了[超想要.jpg] #includ ...
- poj 2960 S-Nim(SG函数)
S-Nim Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3694 Accepted: 1936 Description ...
- Georgia and Bob POJ - 1704 阶梯Nim
$ \color{#0066ff}{ 题目描述 }$ Georgia and Bob decide to play a self-invented game. They draw a row of g ...
- poj 2960 S-Nim【SG函数】
预处理出SG函数,然后像普通nim一样做即可 #include<iostream> #include<cstdio> using namespace std; const in ...
随机推荐
- 201621123043 《Java程序设计》第6周学习总结
1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖面向对象的 ...
- GPUImage实战问题解决
在项目中遇到了使用完GPUImage以后,内存不释放的问题,翻阅官方API,找到了解决方法: deinit{ GPUImageContext.sharedImageProcessingContext( ...
- bzoj千题计划217:bzoj2333: [SCOI2011]棘手的操作
http://www.lydsy.com/JudgeOnline/problem.php?id=2333 读入所有数据,先模拟一遍所有的合并操作 我们不关心联通块长什么样,只关心联通块内有谁 所以可以 ...
- LR回放https协议脚本失败: 错误 -27778: 在尝试与主机“www.baidu.com”connect 时发生 SSL 协议错误
今天用LR录制脚本协议为https协议,回放脚本时出现报错: Action.c(14): 错误 -27778: 在尝试与主机"www.baidu.com"connect 时发生 S ...
- vue style width a href动态拼接问题 ?
style width 这个问题 折磨了我一个上午了 好惭愧 因为项目涉及到 进度条 所以必须用行内样式 style 用过vue的都知道 vue中style的用法 大众用法 :style=&quo ...
- Microsoft dynamic 批量更新
//批量处理 ExecuteMultipleRequest multipleRequest = new ExecuteMultipleRequest() { Settings = new Execut ...
- [UWP] Custom Capability的使用
Custom Capability 是uwp开发中普通开发者较为不常用的内容,但是在一些OEM和驱动厂商,使用频率比较高 Custom Capability 有两种用户: 1.普通应用程序开发者: 2 ...
- CentOS7.4下的 JDK1.8 安装
一.卸载老的JDK 如果需要卸载OpenJDK,执行以下操作: [root@localhost ~]# rpm -e --nodeps tzdata-java-2014i-1.el7.noarch[r ...
- Docker学习笔记 - Docker的数据卷容器
一.什么是数据卷容器 如果你有一些持续更新的数据需要在容器之间共享,最好创建数据卷容器. 数据卷容器:用于容器间的数据共享,主动挂载宿主机目录,用于其他容器挂载和共享. 二.数据卷容器的操作 1.创建 ...
- Linux之Shell命令
开始接触Linux命令行,学习Linux文件系统导航以及创建.删除.处理文件所需的命令. 注:文末有福利! 几个快捷键: Linux发行版通常使用Ctrl+Alt组合键配合F1~F7进入要使用的控制 ...