hdu1536&&hdu3023 SG函数模板及其运用
| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
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
Output
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
WWL
Source
//f[]:可以取走的石子个数
//sg[]:0~n的SG函数值
//hash[]:mex{}
int f[N],sg[N],hash[N];
void getSG(int n)
{
int i,j;
memset(sg,,sizeof(sg));
for(i=;i<=n;i++)
{
memset(hash,,sizeof(hash));
for(j=;f[j]<=i;j++)
hash[sg[i-f[j]]]=;
for(j=;j<=n;j++) //求mes{}中未出现的最小的非负整数
{
if(hash[j]==)
{
sg[i]=j;
break;
}
}
}
}
2dfs模板
dfs传入的参数x为每堆得数量,所以根据题意假如有n堆物品,那么就要进行n次传参就行dfs;
其实sg数组应该在主函数中进行初始化,
我们每次都dfs下一个状态,也就是当前的x状态经过f数组中其中一个取物品方法而得到的另外一种状态,队友回溯的时候我们找到没有被vis标记值
也就是mes集合里面后继点中未出现的最小的正整数值
//注意 S数组要按从小到大排序 SG函数要初始化为-1 对于每个集合只需初始化1遍
//n是集合s的大小 S[i]是定义的特殊取法规则的数组
int s[],sg[],n;
int SG_dfs(int x)
{
int i;
if(sg[x]!=-)
return sg[x];
bool vis[];
memset(vis,,sizeof(vis));
for(i=;i<n;i++)
{
if(x>=s[i])
{
SG_dfs(x-s[i]);
vis[sg[x-s[i]]]=;
}
}
int e;
for(i=;;i++)
if(!vis[i])
{
e=i;
break;
}
return sg[x]=e;
}
下面附上hdu1536的代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
//注意 S数组要按从小到大排序 SG函数要初始化为-1 对于每个集合只需初始化1遍
//n是集合s的大小 S[i]是定义的特殊取法规则的数组
int s[],sg[],n;
int SG_dfs(int x){
int i;
if(sg[x]!=-)
return sg[x];
bool vis[];
memset(vis,,sizeof(vis));
for(i=;i<n;i++){
if(x>=s[i])
{
SG_dfs(x-s[i]);
vis[sg[x-s[i]]]=;
}
}
int e;
for(i=;;i++)
if(!vis[i])
{
e=i;
break;
}
return sg[x]=e;
}
int main()
{
int i,m,t,num;
while(scanf("%d",&n)&&n)
{
for(i=;i<n;i++)
scanf("%d",&s[i]);
memset(sg,-,sizeof(sg));
sort(s,s+n);
scanf("%d",&m);
while(m--)
{
scanf("%d",&t);
int ans=;
while(t--)
{
scanf("%d",&num);
ans^=SG_dfs(num);
}
if(ans==)
printf("L");
else
printf("W");
}
printf("\n");
}
return ;
}
下面同hdu3032
Nim or not Nim?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 623 Accepted Submission(s): 288
Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.
Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.
思路: 此题为博弈中的—取走-分割游戏(这种游戏允许取走某些东西,然后将原来的一个游戏分成若干个相同的游戏)
由于数据范围,不能直接求sg值只能打表找规律;
Lasker's Nim游戏:每一轮允许两会中操作之一:①、从一堆石子中取走任意多个,②、将一堆数量不少于2的石子分成都不为空的两堆。
分析:很明显:sg(0) = 0,sg(1) = 1。
状态2的后继有:0,1和(1,1),他们的SG值分别为0,1,0,所以sg(2) =2。
状态3的后继有:0、1、2、(1,2),他们的SG值分别为0、1、2、3,所以sg(3) = 4。
状态4的后继有:0、1、2、3、(1,3)和(2,2),他们的SG值分别为0,1,2,4,5,0,所以sg(4) = 3.
再推一些,推测得到:对于所有的k >= 0,有 sg( 4k+1 ) = 4k+1; sg( 4k+2 ) = 4k+2; sg( 4k+3 ) = 4k+4; sg( 4k+4 ) = 4k+3。
假设游戏初始时有3堆,分别有2、5和7颗石子。三堆的SG函数值分别为2、5、8,他们的Nim和等于15.所以要走到P状态,就要使得第三堆的SG值变成7,可以将第三对按1和6分成两堆。
下面附上打表代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> using namespace std; const int N=; int sg[N]; int g(int x){
int mex[];
memset(mex,,sizeof(mex));
if(sg[x]!=-)
return sg[x];
for(int i=x-;i>=;i--)//在进入一种状态的时候,我们可以取走任意多的数量,但是不能不取,所以从x-1到0这些情况的mex【g(x)】都应该标记一下
mex[g(i)]=;
for(int i=;i<=x/;i++){
int ans=;
ans^=g(i);//同样一堆我们应该分成两堆,对这两队进行在一起dfs
ans^=g(x-i);
mex[ans]=;
}
for(int i=;;i++)
if(!mex[i])
return sg[x]=i;
} int main(){ //freopen("input.txt","r",stdin); int t,n;
scanf("%d",&t);
memset(sg,-,sizeof(sg));
while(t--){
scanf("%d",&n);
int x;
for(int i=;i<n;i++){
scanf("%d",&x);
g(x);
printf("sg[%d]=%d\n",x,sg[x]);
}
for(int i=;i<=;i++){
printf("%d ",sg[i]);
//if(i%10==0)
//system("pause");
}
printf("\n");
}
return ;
}
通过找规律我们可以总结出下面的代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int ans=;
int n;
scanf("%d",&n);
int x;
for(int i=;i<n;i++){
scanf("%d",&x);
if(x%==||x%==)
ans^=x;
else if(x%==)
ans^=(x+);
else
ans^=(x-);
}
if(ans==)
printf("Bob\n");
else
printf("Alice\n");
}
return ;
}
hdu1536&&hdu3023 SG函数模板及其运用的更多相关文章
- SG函数模板(转)
ps:sg[i]为0表示i节点先手必败. 首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数.例如mex{0,1,2,4}=3.me ...
- hdu 1536 SG函数模板题
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 【非原创】sg函数模板
学习博客:戳这里 解题模型: 1.把原游戏分解成多个独立的子游戏,则原游戏的SG函数值是它的所有子游戏的SG函数值的异或. 即sg(G)=sg(G1)^sg(G2)^...^sg(Gn) ...
- hdu-1536 S-Nim SG函数
http://acm.hdu.edu.cn/showproblem.php?pid=1536 给出能够取的方法序列,然后求基本石子堆问题. 只要用S序列去做转移即可. 注意has初始化的一些技巧 #i ...
- Light OJ 1199:Partitioning Game(SG函数模板)
Alice and Bob are playing a strange game. The rules of the game are: 1. Initially there are n p ...
- HDU 1847-Good Luck in CET-4 Everybody!-博弈SG函数模板
Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此.当然,作为在考场浸润了十几载 ...
- SG函数模板
这篇虽然是转载的,但代码和原文还是有出入,我认为我的代码更好些. 转载自:http://www.cnblogs.com/frog112111/p/3199780.html 最新sg模板: //MAXN ...
- hdu 1536 S-Nim(sg函数模板)
转载自:http://blog.csdn.net/sr_19930829/article/details/23446173 解题思路: 这个题折腾了两三天,参考了两个模板,在这之间折腾过来折腾过去,终 ...
- SG函数 模板
int get_SG(int x) { ) return SG[x]; ]={}; ;i<=n;i++) ) v[get_SG(x-s[i])]=; int i; ;v[i];i++); SG[ ...
随机推荐
- Fresnel Effect
http://www.3drender.com/glossary/fresneleffect.htm http://kylehalladay.com/all/graphics/2014/02/23/F ...
- GPRS/3G
像GPRS/3G模块之类的应用,需要连接,登陆,初始化等步骤完成后才能传输数据,而这些步骤又比较耗时. 所以用 状态机 + 超时 的机制来实现比较合理. 如下代码片段来描述数据透传 : 状态机 + 超 ...
- java 正则 二次转义
JAVA中的正则表达式"\\[([^\\]]+)\\]"这个表示什么意思?两个转义字符是为了表达什么? 正则表达式中"["这样的字符有特殊的意义,所以需要写成& ...
- IP欺骗原理与过程分析
IP欺骗攻击法 原创:r00t <r00t@unsecret.org> QQ: 22664566 http://www.unsecret.org --------------------- ...
- Python之路【第十四篇】前端补充回顾
布局和事件 1.布局 首先看下下面的图片: 上面的内容都是居中的,怎么实现这个效果呢,第一种方法是通过float的方式,第二种是通过“div居中的方式” 第一种方式不在复述了,直接看第二种方式: 1. ...
- knockout-validation不自动插入错误消息
<div data-bind="validationOptions:{insertMessages:false}"> <div class="valid ...
- nlssort函数的用法以及参数
NLSSORT,可以用来进行语言排序,且不影响当前会话. 用法示例: 拼音SELECT * FROM TEAM ORDER BY NLSSORT(排序字段,'NLS_SORT = SCHINESE_P ...
- 【转】【整理】将Linux脚本中的正常输出,警告,错误等信息输出到文件中
本文来自:http://blog.csdn.net/woshinia/article/details/18040063 很早以前 编译的时候 就在用 2>&1,但是一直没有生成一 ...
- Struts2验证框架实例
今天写了个Struts验证框架的实例,总算把验证框架弄清楚了. 上一篇Struts实例的action没有继承ActionSupport类,虽然也可以实现action的功能,但是却不能应用Struts提 ...
- connect() failed (111: Connection refused) while connecting to upstream
配置好lamp后,在浏览器中运行程序后,出现上面的错误. 转自:http://www.xuejiehome.com/blread-1828.html I'm experiencing 502 gate ...