S-Nim

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5637    Accepted Submission(s): 2414

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 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.
 
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
     #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实现)的更多相关文章

  1. hdu 1536 SG函数模板题

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  2. hdu 1848 sg——dfs&&打表双实现

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  3. HDU 1536 sg函数

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

  5. hdu 5724 SG+状态压缩

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  6. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  7. SG 函数初步 HDU 1536 &amp;&amp; HDU 1944

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1944 pid=1536"> http://acm.hdu.edu.cn/showpr ...

  8. HDU 1536 S-Nim SG博弈

    S-Nim Problem Description   Arthur and his sister Caroll have been playing a game called Nim for som ...

  9. hdu 1536 S-Nim(sg函数模板)

    转载自:http://blog.csdn.net/sr_19930829/article/details/23446173 解题思路: 这个题折腾了两三天,参考了两个模板,在这之间折腾过来折腾过去,终 ...

随机推荐

  1. 【实用】Html5实现文件异步上传

    1 简介 开发文件上传功能从来不是一件愉快的事,异步上传更是如此,使用过iframe和Flash的上传方案,也都感觉十分的别扭.本文简要简绍利用Html5的FormData实现文件的异步上传,还可以实 ...

  2. 从零开始利用vue-cli搭建简单音乐网站(七)

    这几天完成了歌曲收藏功能,先看最后效果: 新注册用户:“newuser”,进入“我的音乐界面如下所示” 点击新建歌单,输入:“新歌单”,确认,如下: 目前还没有歌曲,打开音乐界面,点击收藏功能,如下, ...

  3. 引用library之——带有自定义属性的自定义控件的library包

    一般来讲,当自定义一个控件Panel并且此控件有自定义属性时(例如:panel:closedHandle="@drawable/foot_bar_right"),xml中需要定义此 ...

  4. RStudio的Markdown

    Title This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages ...

  5. MyEclipse中把java项目打包——含有第三方jar包【转】

    也适用于eclipse导出jar. 在将项目打包为jar包时一直出现“ClassNotDefFound”错误,百度了很多解决办法都没有解决.最终找到一个很好的解决办法. 1.打包步骤 (1)右键单击j ...

  6. MySQL备份和还原数据库及慢查询日志使用

  7. mvc工作总结

    MVC的页面跳转方式(放在一般类): filterContext.Result = new RedirectResult("controller/action"); filterC ...

  8. Codeforces Round #317 (Div. 2) D Minimization (贪心+dp)

    D. Minimization time limit per test  2 seconds memory limit per test  256 megabytes input  standard ...

  9. mysql 存在更新,不存在插入

    String sql = "insert into wb_result " + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? ...

  10. 解决Genymotion2.8.1在拖动安装APK文件出现ARMtranslate错误

    转载文章:http://blog.csdn.net/solo_talk/article/details/68488129 在新版本的genymotion中,我们拖动安装APK文件的时候会出现一个问题, ...