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 解题思路: 这个题折腾了两三天,参考了两个模板,在这之间折腾过来折腾过去,终 ...
随机推荐
- Spring相关BUG
今天从云开发平台上生成的代码报Spring相关的错误. 我找到第一处错误,整理如下: org.springframework.beans.factory.BeanCreationException: ...
- iOS 常用手势
UIGestureRecognizer 对iOS的各种手势进行了封装,完全满足了用户对手势的需求. 以下是对各种手势的详细应用和说明,希望能对大家有帮助.^_^ - (void)viewDidLoad ...
- android Random的使用
一.Random 此类的实例用于生成伪随机数流.此类使用 48 位的种子,使用线性同余公式 (linear congruential form) 对其进行了修改. 如果用相同的种子创建两个 Rando ...
- uvm_misc——杂货铺(miscellaneous)
uvm_misc 是个很有意思的文件夹,本质上就是个UVM的杂货铺,包含一些很重要的class, task, function, 但不知道给归类到哪儿,所以,uvm_misc就很好地承担了这个任务,几 ...
- Chisel语言
1 What is Chisel? Chisel(Constructing Hardware In a Scala Embedded Language)是一种嵌入在高级编程语言Scala的硬 ...
- iOS 如何解决并发请求时,只接受最后一个请求返回的结果
大致意思是 虽然NSOperation 的cancel 并不能取消请求,但是可以对这个NSOperation进行标记. 当cancel 属性是YES时,表明 NSOperation虽然已经执行,并 ...
- 你不知道的HTTP之首部字段一览
一.HTTP/1.1 首部字段一览 HTTP 首部字段根据实际用途被分为以下 4 种类型: 1.通用首部字段:请求报文和响应报文两方都会使用的首部. 首部字段名 说明 Cache-Control 控制 ...
- NBUT 1117 Kotiya's Incantation(字符输入处理)
题意: 比较两个串,有三种情况:完全相同,可见字符相同,不同.每个字符串以'-'结尾.难点在输入. 思路: 字符逐个读入,直到'-'为止,读出两串就可以直接进行判断.如果不足两串则结束.输入时需要注意 ...
- hadoop ssh 问题WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
0.0.0.0: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@0.0.0.0: @ WARNING: REMOTE HO ...
- 使用javap分析Java的字符串操作
我们看这样一行简单的字符串赋值操作的Java代码. String a = "i042416"; 使用命令行将包含了这行代码的Java类反编译查看其字节码: javap -v con ...