hdu 1536 sg (dfs实现)
S-Nim
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5637 Accepted Submission(s): 2414
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.
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.
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.
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
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 100+10
int knum,mnum,lnum;
int ans[N],si[N],hi[N],sg[];
int mex(int x)//求x的sg值(可作为模版应用)
{
if(sg[x]!=-) return sg[x];
bool vis[N];
memset(vis,false,sizeof(vis));
for(int i=;i<knum;i++) {
int temp=x-si[i];
if(temp<) break;
sg[temp]=mex(temp);
vis[sg[temp]]=true;
}
for(int i=;;i++) {
if(!vis[i]) {
sg[x]=i; break;
}
}
return sg[x];
}
int main() {
while(scanf("%d",&knum) && knum) {
for(int i=;i<knum;i++)
scanf("%d",&si[i]);
sort(si,si+knum);
memset(sg,-,sizeof(sg));
sg[]=;
memset(ans,,sizeof(ans));
scanf("%d",&mnum);
for(int i=;i<mnum;i++) {
scanf("%d",&lnum);
for(int j=;j<lnum;j++) {
scanf("%d",&hi[i]); ans[i]^=mex(hi[i]);//尼姆博弈
}
}
for(int i=;i<mnum;i++)
{
if(ans[i]==) printf("L");
else printf("W");
}
printf("\n");
}
return ;
}
#include"iostream"
#include"algorithm"
#include"string.h"
using namespace std;
int s[],sg[],k;
int getsg(int m)
{
int hash[]={};
int i;
for(i=;i<k;i++){
if(m-s[i]<)
break;
if(sg[m-s[i]]==-)
sg[m-s[i]]=getsg(m-s[i]);
hash[sg[m-s[i]]]=;
}
for(i=;;i++)
if(hash[i]==)
return i; }
int main()
{
//int k;
// freopen("game.in","r",stdin);
//freopen("game.out","w",stdout);
while(cin>>k,k)
{
int i;
for(i=;i<k;i++)
cin>>s[i];
sort(s,s+k);
memset(sg,-,sizeof(sg));
sg[]=;
int t;
cin>>t;
while(t--)
{ int n,m;
cin>>n;
int ans=;
while(n--)
{
cin>>m;
if(sg[m]==-)
sg[m]=getsg(m);
ans^=sg[m];
}
if(ans)
cout<<'W';
else cout<<'L';
}
cout<<endl;
}
return ;
}
hdu 1536 sg (dfs实现)的更多相关文章
- hdu 1536 SG函数模板题
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- hdu 1848 sg——dfs&&打表双实现
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- HDU 1536 sg函数
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- S-Nim HDU 1536 博弈 sg函数
S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...
- hdu 5724 SG+状态压缩
Chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- SG 函数初步 HDU 1536 && HDU 1944
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1944 pid=1536"> http://acm.hdu.edu.cn/showpr ...
- HDU 1536 S-Nim SG博弈
S-Nim Problem Description Arthur and his sister Caroll have been playing a game called Nim for som ...
- hdu 1536 S-Nim(sg函数模板)
转载自:http://blog.csdn.net/sr_19930829/article/details/23446173 解题思路: 这个题折腾了两三天,参考了两个模板,在这之间折腾过来折腾过去,终 ...
随机推荐
- 死磕 java并发包之AtomicStampedReference源码分析(ABA问题详解)
问题 (1)什么是ABA? (2)ABA的危害? (3)ABA的解决方法? (4)AtomicStampedReference是什么? (5)AtomicStampedReference是怎么解决AB ...
- Eclipse+Tomcat环境集成
1.下载Eclipse: 我用的Version: Mars.2 Release (4.5.2),直接在官网上下:http://www.eclipse.org/downloads/packages/re ...
- System.TypeInitializationException: 'The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.'
下午在调试的时候报错数据库连接就报错我就很纳闷后面用原来的代码写发现还是报错 System.TypeInitializationException: 'The type initializer for ...
- SQL 基本编程
定义变量 赋值 取值 分支语句 循环语句 定义变量 declare @变量 数据类型 //@必须带着 不然程序不知道变量是什么 不带@ 电脑会报错 例如 declare ...
- haml scss转换编写html css的前期工作
http://www.w3cplus.com/sassguide/install.html 先下载ruby $ gem sources $ gem sources --remove https://r ...
- Java poi 的使用
poi可操作老旧版本的excel 下载jar包,http://archive.apache.org/dist/poi/release/bin/poi-bin-3.17-20170915.tar.gz ...
- iis隐藏index.php
1.先安装微软的URL Rewrite模块 网址是https://www.iis.net/downloads/microsoft/url-rewrite#additionalDownloads 安装完 ...
- 661. Image Smoother@python
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- python中with用法及原理
资源的管理在程序的设计上是一个很常见的问题,例如管理档案,开启的网络socket与各种锁定(locks)等.最主要的问题在于我们必须确保这些开启的资源在使用之后能够关闭(或释放),若忘记关闭这些资源, ...
- (12)zabbix agent 类型所有key
zabbix服务器端通过与zabbix agent通信来获取客户端服务器的数据,agent分为两个版本,其中一个是主动一个是被动,在配置主机我们可以看到一个是agent,另一个是agent(activ ...