S-Nim

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7262    Accepted Submission(s): 3074

Problem 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
 
Source
题意:m堆石子  玩家每次可以从某一堆中取出si个石子 不能取则输
题解:初步学习sg函数 sg[i]为 i的后继状 的sg值中 没有出现过的非负最小值。
sg异或值为0则后手胜
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int k;
int sg[];
int a[];
int flag[];
int q,m,exm;
void init()
{
sg[]=;
for(int i=;i<=;i++)
{
memset(flag,,sizeof(flag));
for(int j=;j<=k;j++)
{
if(i-a[j]>=)
{
flag[sg[i-a[j]]]=;
}
}
for(int j=;;j++)
{
if(flag[j]==){
sg[i]=j;
break;
}
}
}
}
int main()
{
while(scanf("%d",&k)!=EOF)
{
if(k==)
break;
for(int i=;i<=k;i++)
scanf("%d",&a[i]);
init();
scanf("%d",&q);
for(int i=;i<=q;i++)
{
scanf("%d",&m);
int ans=;
for(int j=;j<=m;j++)
{
scanf("%d",&exm);
ans^=sg[exm];
}
if(ans==)
printf("L");
else
printf("W");
}
printf("\n");
}
return ;
}

HDU 1536 sg函数的更多相关文章

  1. hdu 1536 SG函数模板题

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  2. hdu 2147 SG函数打表(手写也可以) 找规律

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/1000 K (Java/Others) Total ...

  3. hdu 1848(SG函数)

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  4. hdu 1847(SG函数,巴什博弈)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. HDU 1848 SG函数博弈

    Fibonacci again and again Problem Description   任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1 ...

  6. hdu 2999 sg函数(简单博弈)

    Stone Game, Why are you always there? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/ ...

  7. hdu 1536 sg (dfs实现)

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. SG 函数初步 HDU 1536 &amp;&amp; HDU 1944

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1944 pid=1536"> http://acm.hdu.edu.cn/showpr ...

  9. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

随机推荐

  1. Python学习笔记(2)

    变量 变量名就像我们现实社会的名字,把一个值赋值给一个名字时,它会存储在存储中,称之为变量(Variable),在大多数语言中,都把这种行为称为“给变量赋值”或“把值存储在变量中”. 而Python与 ...

  2. 今天的感悟,对于python中的list()与w3c教程

    首先本来想百度一下python定义列表的时候用 list()与直接用[]有什么区别,其中没有找到相关直接资料,看到了W3c菜鸟教程中之前看到的tuple,不禁想起list(tuple)是用来将元组转换 ...

  3. bootstrap selectpicker

    mark 一下使用 bootstrap selectpicker 遇到的一个小issue,作为下次查错使用 $('.selectpicker').selectpicker('val', 'Mustar ...

  4. Server asks us to fall back to SIMPLE auth, but this client is configured to only allow secure connections.

    我是在flume向hdfs 写(sink)数据时遇到的这个错误. Server (是指hdfs) asks us to fall back to SIMPLE auth, but this clien ...

  5. 课时9—popup

    内容比较多的时候可以全屏显示,用第一种实现方式,如图1 图1 内容较少的话可以使用第二种实现方式如图2:图2 具体的实现代码如下: .header,.footer,.wrap-page{ positi ...

  6. Grunt自动化构建工具(网址:http://www.gruntjs.net/getting-started或者http://gruntjs.cn/getting-started)

    简介:Grunt是基于Node.js的项目构建工具,对于需要重复执行的任务,例如压缩.编译.单元测试等,自动化工具可以减少你的工作量,使你的工作更轻松. 一:检测nodejs是否安装好,打开CMD控制 ...

  7. Druid(准)实时分析统计数据库——列存储+高效压缩

    Druid是一个开源的.分布式的.列存储系统,特别适用于大数据上的(准)实时分析统计.且具有较好的稳定性(Highly Available). 其相对比较轻量级,文档非常完善,也比较容易上手. Dru ...

  8. BZOJ4046 [Cerc2014] Pork barre

    我们把边按权值从大到小依次加入图中 如果加到边权$V$,则当前的最小生成森林中边权$v\in[V, V']$(其中$V'$是任意值)形成的森林的边权和就是对于询问$[V, V']$的答案 由于点数不多 ...

  9. js框架简明

    jquery 主要战场还是在dom这块.其它经典怀旧的2个需要了解一下,mootools, prototype.是他们启发了js向工程化,团队化,协作化发展的转变,yui虽然听说停止开发了,但他的代码 ...

  10. linux ps 命令

    ps命令是用来查看系统运行的进程 -A 显示所有进程(等价于-e)(utility) -a 显示一个终端的所有进程,除了会话引线 -N 忽略选择. -d 显示所有进程,但省略所有的会话引线(utili ...