S-Nim
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 rst 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

用dfs搜索sg值

 #include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int a[];
int t;
int b[];
int dfs(int x)
{
if(b[x]!=-)return b[x];
int i;
if(x-a[]<)return ;
int c[];
memset(c,,sizeof(c));
for(i=;i<t;i++)
{
if(x-a[i]<)
{
break;
}
c[dfs(x-a[i])]=;
}
for(i=;i<;i++)
if(c[i]==)
{
b[x]=i;
break;
}
return b[x];
}
int main()
{
while(cin>>t&&t){
memset(a,,sizeof(a));
memset(b,-,sizeof(b));
int j;
for(j=;j<t;j++)
cin>>a[j];
sort(a,a+t);
int i;
b[]=;
int n;
cin>>n;
for(i=;i<n;i++)
{
int m;
int sum=,x;
cin>>m;
for(j=;j<m;j++)
{
cin>>x;
sum^=dfs(x);
}
if(sum)
cout<<"W";
else cout<<"L";
}
cout<<endl;
}
}

hdu1356&hdu1944 博弈论的SG值(王道)的更多相关文章

  1. 题解报告:hdu 1847 Good Luck in CET-4 Everybody!(入门SG值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1847 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧 ...

  2. 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)

    [题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...

  3. CF 256C Furlo and Rublo and Game【博弈论,SG函数】

    暴力的求SG函数会超时,正解是先处理出10^6以内的SG值,对于更大的,开根号之后计算出. 小数据观察可以发现sg函数值成段出现,而且增长速度很快,因此可以计算出来每一段的范围,只需打表即可. Nim ...

  4. 博弈论与SG函数

    巴什博奕: 两个顶尖聪明的人在玩游戏,有n个石子,每人可以随便拿1−m个石子,不能拿的人为败者,问谁会胜利 结论: 设当前的石子数为\(n=k∗(m+1)\)即\(n%(m+1)==0\)时先手一定失 ...

  5. hdu5795 A Simple Nim 求nim求法,打表找sg值规律 给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作可以选择任意一堆取走任意个石子(不可以为空) 或者选择一堆,把它分成三堆,每堆不为空。求先手必胜,还是后手必胜。

    /** 题目:A Simple Nim 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作 ...

  6. poj 3537 Crosses and Crosses 博弈论之grundy值

    题意: 给1*n的格子,轮流在上面叉叉,最先画得3个连续叉叉的赢.问先手必胜还是必败. 分析: 求状态的grundy值(也就是sg值),详细怎么求详见代码.为什么这么求要自己想的,仅仅可意会(别人都说 ...

  7. 0x3A 博弈论之SG函数

    博弈即玄学啊 (除了nim和二分图博弈什么都不会 算是学了下SG函数吧 这个东西是针对有向图游戏的,相当于把一个局面看作一个点,到达下个局面相当于建一条边 必胜态SG值为0 那么对于一个点,他的SG值 ...

  8. ABC206 F - Interval Game 2 (区间DP,博弈论,SG函数)

    题面 题意很简单 A l i c e \tt Alice Alice 和 B o b \tt Bob Bob 在博弈.摆在他们面前有 N \rm N N 个区间 [ l i , r i ) \rm[l ...

  9. 博弈论(SG函数):HNOI 2007 分裂游戏

    Description 聪聪和睿睿最近迷上了一款叫做分裂的游戏. 该游戏的规则试: 共有 n 个瓶子, 标号为 0,1,2.....n-1, 第 i 个瓶子中装有 p[i]颗巧克力豆,两个人轮流取豆子 ...

随机推荐

  1. HTTP协议Keep-Alive模式详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp22 HTTP协议Keep-Alive模式详解 1.什么是Keep-Aliv ...

  2. oracle 索引失效的情况分析

    见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp54     1) 没有查询条件,或者查询条件没有建立索引 2) 在查询条件上 ...

  3. oracle12之 多租户容器数据库架构

    解读: 这张幻灯片展示了三个被部署的应用程序的整合 三个不同的非cdbs成为一个单一的.幻灯片中的图形显示了一个多租户 容器数据库有四个容器:根和三个可插入的数据库.每一个 可插入数据库有它自己的专用 ...

  4. 【深入Java虚拟机】之七:Javac编译与JIT编译

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/18009455 编译过程 不论是物理机还是虚拟机,大部分的程序代码从开始编译到最终转化成物理 ...

  5. 转:【深入Java虚拟机】之五:多态性实现机制——静态分派与动态分派

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17965867   方法解析 Class文件的编译过程中不包含传统编译中的连接步骤,一切方法 ...

  6. JAVA基础第七组(5道题)

    31.题目:将一个数组逆序输出.              1.程序分析:用第一个与最后一个交换. package com.niit.homework1; /** * @author: Annie * ...

  7. 201521123030《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 控制可见性的4个访问修饰符 private -- 仅对本类可见 public -- 对所有类 ...

  8. 201521123061 《Java程序设计》第三周学习总结

    1. 本章学习总结 2. 书面作业 **Q1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2 ...

  9. 201521123105 第三周Java学习总结

    1. 本周学习总结 对象(实际个体) 对象与类 类(模板) 2.书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private ...

  10. 201521123027 《JAVA程序设计》第二周学习总结

    1.本周学习总结 1.学习使用码云管理程序代码: 2.认识类型(整数.字节.浮点数.字符与布尔)与变量的使用: 3.学习运算符的基本使用以及类型转换的基本规则: 4.学习String类的一些使用方法: ...