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[ ...
随机推荐
- Eclipse学习笔记——快捷键
alt+/ 直接呼出要重写的方法 ctrl+1 快速修复(没导包,没抛出异常等等) ctrl+shift+o 导包 ctrl+shift+F 格式化代码块(自动给代码排版) alt+方向键 向前向后( ...
- git 删除远程分支
http://www.cnblogs.com/shiningrise/archive/2013/03/12/2956779.html 一不小心把本地的临时分支push到server上去了,想要删除.一 ...
- 一个 Sql语句优化的问题- STATISTICS 统计信息
前段时间,同事遇到一个 Sql语句的问题,一个列表分页功能响应在30 s以上,看数据库里面的数据条数,数据量也不大,相关字段的一些索引也都有,可就是慢.于是找出具体的sql 语句出来分析,分页功能主要 ...
- chrome调试文章
http://blog.csdn.net/a6225301/article/details/20207191#t1 http://www.360doc.com/content/13/1220/11/8 ...
- Asp.Mvc中的text实现 辅助用户输入 灰色字体
在开发Web应用程序中经常需要用户在文本框输入信息,为了提高程序人性化设置以及用户体验效果常常需要在文本框中显示灰色字体辅助用户输入 如:
- Java Programming Test Question 2
public class JPTQuestion2 { public static void main(String[] args) { String s3 = "JournalDev&qu ...
- css使 同一行内的 文字和图片 垂直居中对齐?
设置父容器, 使 父容器 div 下的所有元素 都 垂直对齐: father-container *{ vertical-align:middle 找回密码
- yum clean all 是什么意思
yum会将下载下来的 包文件rpm和头文件header存盘在 本地机器的硬盘 缓存中, 这个将占用 硬盘空间, 可以将这些内容清除掉, 以释放磁盘空间: yum clean headers: // 释 ...
- 单例模式singleton
在进行开发的时候,我们在有些情形下有些对象我们只需要一个.例如:配置文件.工具类.线程池.缓存.日志对象等. 如何保证我们的对象只有一个呢?我们可以通过单例来实现. 常用的单例有两种:饿汉模式和懒汉模 ...
- 【AngularJS】—— 11 指令的交互
前面基本了解了指令的相关内容: 1 如何自定义指令 2 指令的复用 本篇看一下指令之间如何交互.学习内容来自<慕课网 指令3> 背景介绍 这例子是视频中的例子,有一个动感超人,有三种能力, ...