杭电 1536(尼姆博弈)

S-Nim

<span size="+0"><strong><span style="font-family:Arial;font-size:12px;color:green;FONT-WEIGHT: bold">Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4479    Accepted Submission(s): 1941
</span></strong></span>
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
 

Source
Norgesmesterskapet 2004
 
思路:尼姆博弈的变形,套用了sg函数
代码如下:
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int k,a[110],f[10010];
int mex(int p)//sg函数
{
	int i,t;
	bool g[110]={0};
	for(i=0;i<k;i++)
	{
		t=p-a[i];
		if(t<0)
		break;
		if(f[t]==-1)
		f[t]=mex(t);
		g[f[t]]=1;
	}
	for(i=0;;i++)
	if(!g[i])
	return i;
}
int main()
{
	int n,i,m,j,t,s;
	while(~scanf("%d",&k),k)
	{
		for(i=0;i<k;i++)
		scanf("%d",&a[i]);
		sort(a,a+k);
		memset(f,-1,sizeof(f));
		f[0]=0;
		scanf("%d",&n);
		while(n--)
		{
			scanf("%d",&m);
			s=0;
			while(m--)
			{
				scanf("%d",&t);
				if(f[t]==-1)
				f[t]=mex(t);
				s^=f[t];//尼姆博弈,各个堆的数量的异或和为零的话,先手输
			}
			if(s==0)
			printf("L");
			else
			printf("W");//题上的没有换行,注意细节 ,此处没有换行符
		}
		printf("\n");
	}
	return 0;
}

杭电 1536(尼姆博弈)

时间: 2024-10-08 09:47:49

杭电 1536(尼姆博弈)的相关文章

杭电 1850(尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4649    Accepted Submission(s): 2774 Problem Description 一年在外 父母时刻牵挂 春节回家 你能做几天好孩子吗 寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场 悄悄

Being a Good Boy in Spring Festival(杭电1850)(尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4894    Accepted Submission(s): 2930 Problem Description 一年在外 父母时刻牵挂 春节回家 你能做几天好孩子吗 寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场 悄悄

hdu 1907 尼姆博弈

John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3745    Accepted Submission(s): 2116 Problem Description Little John is playing very funny game with his younger brother. There is one big bo

hdu 1850 Being a Good Boy in Spring Festival (尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4658    Accepted Submission(s): 2781 Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场悄悄给爸爸买

Being a Good Boy in Spring Festival(尼姆博弈)

Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 51 Accepted Submission(s): 45   Problem Description 一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场悄悄给爸爸买个小礼物主动地

最强头脑决定战中的尼姆博弈

最近看了一个日本综艺——最强头脑王决定战,顿觉以前看过的国内烧脑节目,类似“一站到底”之流与之相比真是相形见绌.“一站”说到底只是比谁题库背得熟,而日本这个真的是记忆.计算.观察.推理等各种能力的综合考察,比赛选手们各显神通,观众如我看得也是如痴如醉. 其中比赛到第二轮时有一道题如下:有四堆不同色的棋子,每堆分别有3.5.6.7枚,由选手和电脑开始依次从其中取出一些棋子,每次只可取同一堆的任意枚,如此交替直至取完所有棋子,谁取到最后一枚棋子谁输. 当时水上小哥不假思索地取子很轻松就赢了,让评委们

nyoj 取石子(三)(尼姆博弈的定理运用)

取石子(三) 时间限制:1000 ms |  内存限制:1000 KB 难度:6 描述 小王喜欢与同事玩一些小游戏,今天他们选择了玩取石子. 游戏规则如下:共有N堆石子,已知每堆中石子的数量,两个人轮流取子,每次只能选择N堆石子中的一堆,取一定数量的石子(最少取一个),取过子之后,还可以将该堆石子中剩下的任意多个石子中随意选取几个放到其它的任意一堆或几堆上.等哪个人无法取子时就表示此人输掉了游戏.注意,一堆石子没有子之后,就不能再往此处放石子了. 假设每次都是小王先取石子,并且游戏双方都绝对聪明

hdu 1849(Rabbit and Grass) 尼姆博弈

Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3864    Accepted Submission(s): 2919 Problem Description 大学时光是浪漫的,女生是浪漫的,圣诞更是浪漫的,但是Rabbit和Grass这两个大学女生在今年的圣诞节却表现得一点都不浪漫:不去逛商场,不去逛

HDU 4315 Climbing the Hill (尼姆博弈)

Climbing the Hill Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Alice and Bob are playing a game called "Climbing the Hill". The game board consists of cells arranged vertically, as the