Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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
 

Source

Norgesmesterskapet 2004
此题题意sg问题的入门题,第一行第一个数代表着s集合的个数,s集合代表着我们一次可以取得物品的个数,后面紧跟着k个数
第二行有一个数代表着询问次数,
接下面每行第一行有一个数代表着总的堆数n,后面紧跟着n个数,分别代表每个堆物品的数量,
问谁先取光谁获胜,
典型的SG问题,
下面附有SG函数的模板
1打表模板
//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

Problem Description
Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.

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.

 
Input
Input contains multiple test cases. The first line is an integer 1 ≤ T ≤ 100, the number of test cases. Each case begins with an integer N, indicating the number of the heaps, the next line contains N integers s[0], s[1], ...., s[N-1], representing heaps with s[0], s[1], ..., s[N-1] objects respectively.(1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1)
 
Output
For each test case, output a line which contains either "Alice" or "Bob", which is the winner of this game. Alice will play first. You may asume they never make mistakes.
 
Sample Input
2
3
2 2 3
2
3 3
 
Sample Output
Alice Bob
 
Source
 
Recommend
第一行输入一个正整数t代表case
第二行代表有n个堆,、
下面n个整数代表每堆中物品的数量
题目的要求的可以有两种操作,一种是从一个堆中取走任意数量的物品,第二种就是将一堆分成两个小堆

思路: 此题为博弈中的—取走-分割游戏(这种游戏允许取走某些东西,然后将原来的一个游戏分成若干个相同的游戏)

由于数据范围,不能直接求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函数模板及其运用的更多相关文章

  1. SG函数模板(转)

    ps:sg[i]为0表示i节点先手必败. 首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数.例如mex{0,1,2,4}=3.me ...

  2. hdu 1536 SG函数模板题

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

  3. 【非原创】sg函数模板

    学习博客:戳这里 解题模型: 1.把原游戏分解成多个独立的子游戏,则原游戏的SG函数值是它的所有子游戏的SG函数值的异或.        即sg(G)=sg(G1)^sg(G2)^...^sg(Gn) ...

  4. hdu-1536 S-Nim SG函数

    http://acm.hdu.edu.cn/showproblem.php?pid=1536 给出能够取的方法序列,然后求基本石子堆问题. 只要用S序列去做转移即可. 注意has初始化的一些技巧 #i ...

  5. 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 ...

  6. HDU 1847-Good Luck in CET-4 Everybody!-博弈SG函数模板

    Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Cici都是如此.当然,作为在考场浸润了十几载 ...

  7. SG函数模板

    这篇虽然是转载的,但代码和原文还是有出入,我认为我的代码更好些. 转载自:http://www.cnblogs.com/frog112111/p/3199780.html 最新sg模板: //MAXN ...

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

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

  9. SG函数 模板

    int get_SG(int x) { ) return SG[x]; ]={}; ;i<=n;i++) ) v[get_SG(x-s[i])]=; int i; ;v[i];i++); SG[ ...

随机推荐

  1. Java递归算法——三角数字

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...

  2. Unity Serialization

    http://forum.unity3d.com/threads/serialization-best-practices-megapost.155352/ http://docs.unity3d.c ...

  3. VS2013快捷键大全

    Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...

  4. nginx basic auth 登陆验证模块

    #1. 新建一个pw.pl文件专门用来生成密码 #!/usr/bin/perl use strict; my $pw=$ARGV[0]; print crypt($pw,$pw)."\n&q ...

  5. 单元测试写cookie

    我们在开发WEB项目的时候,一般应用逻辑跟ASPX页面是分离的项目.应用逻辑一般会是一个DLL组件项目.如果这个组件项目中A方法使用了Session.Cookie等信息的读写,则这个方法就很难写单元测 ...

  6. Python-面向对象编程(二)

    面向对象进阶篇: 初级篇中我们介绍了面向对象基本知识: 1.面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 2.介绍了类中的对象.方法和属性及类中内置的方法 3.类 是一个模板 ...

  7. Yii2创建多界面主题(Theme)

    Yii2界面主题上的设计总体上和Yii1.x保持一致,区别在于两个地方: 1. 由于Yii2引入了独立的视图(View)类,因此界面主题(Theme)也交由视图来管理: 2. 视图文件和Web资源在目 ...

  8. 【10-25】OOP基础-飞机游戏知识点

    知识点 鼠标适配器类为抽象类,使用匿名子类实现鼠标事件的重写,创建一个鼠标适配器对象 添加鼠标事件监听器到JPanel对象实现鼠标的响应 创建定时器Timer对象,在定时器的任务列表方法schedul ...

  9. hadoop常见问题汇集

    1 hadoop conf.addResource http://stackoverflow.com/questions/16017538/how-does-configuration-addreso ...

  10. Java字节流:InputStream OutputStream

    字节输入流:InputStream 类声明: public abstract class InputStream implements Closeable 位于java.io包下,是一个抽象类. 官方 ...