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 解题思路: 这个题折腾了两三天,参考了两个模板,在这之间折腾过来折腾过去,终 ...
随机推荐
- 前端之CSS布局模型
一.css布局模型: 流动模型(Flow) 浮动模型(Float) 层模型(Layer) 1.流动模型: 页面在没有设置任何css样式,元素按照本身的特性在浏览器中显示,这样的布局模型称为流动模型: ...
- poj2135 最小费用流
添加超级源点(与点1之间的边容量为2,权值为0)和超级汇点(与点N之间的边容量为2,权值为0),求流量为2的最小费用流.注意是双向边. #include <iostream> #inclu ...
- Android Generate Signed APK: Errors while building APK. You can find the errors )
开发过程中,总会遇到很多坑: Gradle build finished with 101 error(s) in 1m 35s 424ms 19:23:50 Generate Signed APK: ...
- JavaScript数据格式验证探讨
1.需求 修改某个文本框数据,要求对修改后的格式做验证(必须是数字). 注:实际需求比上述复杂,为了说明问题,这里特意简化了需求(如:对修改后数据依赖条件的判断,数据入库等). 2.关于NaN的探讨( ...
- 洛谷 P1311 选择客栈
题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...
- 关于bootstrap栅格系统的五等分以及八等分代码
众所周知,bootstrap的栅格系统是基于十二等分的,今天拿到设计的设计稿一看,发现一个图片list上只有8张图片,然后上网查资料,发现只能自己写css代码实现,故写博客记录代码. 以下是八等分的代 ...
- k8s master init and add node
目录 一. add google apt-key 二. k8s master init 三. k8s node add to master cluster(use this command when ...
- 在Phonegap下实现oAuth认证
原文:http://www.kuqin.com/mobile/20120719/322873.html 前段时间做过两次关于Phonegap的现场交流会议分享.基本上把Phonegap的一些特性和大家 ...
- PLAYGROUND 延时运行
PLAYGROUND 延时运行 由 王巍 (@ONEVCAT) 发布于 2015/09/16 从 WWDC 14 的 Keynote 上 Chris 的演示就能看出 Playground 异常强大,但 ...
- 【树形dp】bzoj1304: [CQOI2009]叶子的染色
又是一道优美的dp Description 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白色.你的着色方案应该保证根结点到每个叶子的 ...