hdu1356&hdu1944 博弈论的SG值(王道)
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值(王道)的更多相关文章
- 题解报告:hdu 1847 Good Luck in CET-4 Everybody!(入门SG值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1847 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧 ...
- 【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)
[题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ...
- CF 256C Furlo and Rublo and Game【博弈论,SG函数】
暴力的求SG函数会超时,正解是先处理出10^6以内的SG值,对于更大的,开根号之后计算出. 小数据观察可以发现sg函数值成段出现,而且增长速度很快,因此可以计算出来每一段的范围,只需打表即可. Nim ...
- 博弈论与SG函数
巴什博奕: 两个顶尖聪明的人在玩游戏,有n个石子,每人可以随便拿1−m个石子,不能拿的人为败者,问谁会胜利 结论: 设当前的石子数为\(n=k∗(m+1)\)即\(n%(m+1)==0\)时先手一定失 ...
- hdu5795 A Simple Nim 求nim求法,打表找sg值规律 给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作可以选择任意一堆取走任意个石子(不可以为空) 或者选择一堆,把它分成三堆,每堆不为空。求先手必胜,还是后手必胜。
/** 题目:A Simple Nim 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作 ...
- poj 3537 Crosses and Crosses 博弈论之grundy值
题意: 给1*n的格子,轮流在上面叉叉,最先画得3个连续叉叉的赢.问先手必胜还是必败. 分析: 求状态的grundy值(也就是sg值),详细怎么求详见代码.为什么这么求要自己想的,仅仅可意会(别人都说 ...
- 0x3A 博弈论之SG函数
博弈即玄学啊 (除了nim和二分图博弈什么都不会 算是学了下SG函数吧 这个东西是针对有向图游戏的,相当于把一个局面看作一个点,到达下个局面相当于建一条边 必胜态SG值为0 那么对于一个点,他的SG值 ...
- 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 ...
- 博弈论(SG函数):HNOI 2007 分裂游戏
Description 聪聪和睿睿最近迷上了一款叫做分裂的游戏. 该游戏的规则试: 共有 n 个瓶子, 标号为 0,1,2.....n-1, 第 i 个瓶子中装有 p[i]颗巧克力豆,两个人轮流取豆子 ...
随机推荐
- asp.net core策略授权
在<asp.net core认证与授权>中讲解了固定和自定义角色授权系统权限,其实我们还可以通过其他方式来授权,比如可以通过角色组,用户名,生日等,但这些主要取决于ClaimTypes,其 ...
- oracle时间范围查询
当时间精确到秒的指标和时间精确到日的时间进行对比是恒不等于的,但是可以判断大于或者等于的情况. 举个例子,指标[时间精确到秒] select 时间精确到秒 from table where 时间精确到 ...
- macaca 测试web(2)
还是以博客园登录为例, 可以查看上一篇上一篇 这次只测试登录,使用ddt的数据驱动,这里我博客也有记录 地址 其实我在uiautomator +python 实现安卓自动化 一文当中我已经使用过ddt ...
- Java基础精选,你答对了几道?
没有技术深度是大多程序员的一种常态. 但是当你成为一个资深的工程师的时候,很多公司并不希望你还是那样平庸,没有深度.虽然你会纳闷,我就算有深度你们也不一定用得上呀?然而到了这个级别的人需求量并不像初中 ...
- 改变input光标颜色与输入字体颜色不同
设置input css: color #ffd600text-shadow 0px 0px 0px #bababa -webkit-text-fill-color initial input, tex ...
- Unity2017.x 版本的下载安装
Unity2017 版本从2017年7月开始正式发布Unity2017.1版本,为了初学者更加清晰明了的掌握全过程,笔者还是以完整的共19个步骤来给大家演示下载与配置安装的全过程,方便广大读者的无障碍 ...
- 如何在sublime+chrome中调试php代码?
1.搭建php本地运行环境具体点击如何使用phpstudy本地搭建多站点(每个站点对应不同的端口) 2.下载php_xdebug.dll, [5.3版以上的php下载地址]http://pecl.ph ...
- MongoDB学习之路(二)
MongDB特点 MongoDB是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器的性能. MongoDB旨在为WEB应用提供可拓展的高 ...
- 201521123090 《Java程序设计》第7周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代 ...
- evak购物车--团队博客
1. 团队名称.团队成员介绍(需要有照片) 团队名称:evak 团队成员介绍:陈凯欣,计算机工程学院网络工程1512,学号为201521123034:邱晓娴,计算机工程学院网络工程1512,学号为20 ...