hdu 1536 S-Nim|| poj 2960 S-Nim (sg函数)

#include <stdio.h>
#include <string.h>

int s[110];
int sg[10010],hash[110];
int n, m;

int getsg(int x)  //sg模板
{
    int i;
	if(sg[x] != -1)
		return sg[x];

	memset(hash,0,sizeof(hash));
	for(i = 0; i < n; i++)
	{
		if(x >= s[i])
		{
			sg[x - s[i]] = getsg(x - s[i]);
			hash[sg[x - s[i]]] = 1;
		}
	}
	for(i = 0; i < 110; i++)
	{
		if(hash[i] == 0)
			break;
	}
	return i;
}

int main ( )
{
	int i,k;
	int a,j,l;
	char ans[110];

	while(scanf("%d", &n) != EOF)
	{
		if(n == 0)
			break;
	      k = 0;

		for(i = 0; i < n; i++)
			scanf("%d", &s[i]);

		memset(sg, -1, sizeof(sg));
		sg[0] = 0;
		for(i = 1; i < 10010; i++)
			sg[i] = getsg(i);

		scanf("%d", &m);
		for(i = 0; i < m; i++)
		{
			int sum = 0;
			scanf("%d", &l);
			for(j = 0; j < l; j++)
			{
				scanf("%d", &a);
				sum ^= sg[a];   //尼姆的变形
			}
			if(sum == 0)
				ans[k ++] = 'L';
			else
				ans[k ++] = 'W';
		}
		for(i = 0; i < k; i++)
			printf("%c", ans[i]);
		printf("\n");
	}
	return 0;
}

hdu 1536 S-Nim|| poj 2960 S-Nim (sg函数)

时间: 2024-10-12 02:19:41

hdu 1536 S-Nim|| poj 2960 S-Nim (sg函数)的相关文章

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now

poj 2960 S-Nim(SG函数)

S-Nim Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3694   Accepted: 1936 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 h

POJ 2960 S-Nim 博弈论 sg函数

http://poj.org/problem?id=2960 sg函数几乎是模板题. 调试代码的最大障碍仍然是手残在循环里打错变量名,是时候换个hydra产的机械臂了[超想要.jpg] 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<iostream> 6 #include<map> 7 using na

POJ 2960 S-Nim(SG函数模板题)

链接:https://vjudge.net/problem/POJ-2960 题意:每行输入首先给出一个数k,代表集合S的大小,接下来紧跟着k个数,表示集合S里的数.接下来一行数为m代表有m个游戏,后面m行每行第一个数字为n代表有n堆石子,后面紧跟着n个数代表每堆石子的个数.多组数据,做到0结束 对于每组数据,我们要输出n个字母,第i个字母为“W”代表第i个游戏先手必胜,“L”代表第i个游戏先手必败,做完一组数据后换行. 题解:模板题 #include <cstdio> #include &l

HDU 3032 Nim or not Nim? (博弈之求SG函数)

题意:经典Nim博弈游戏变换,给你n堆石子pi,每堆有pi个石子, Alice和Bob轮流取石子,每次可以从任意一堆中拿走任意个石子,也可以将某一堆石子分成两个小堆 (每堆石子个数必须不能为0),先拿完者获胜 思路:求SG函数后找规律: SG函数定义及求法:点击打开链接 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in

poj 2960 S-Nim nim博弈grundy值计算法入门

题意: 给k堆石子,两人轮流向某一堆中拿,拿的个数要从给定的一个集合中取,没石子拿的输,问先手必胜还是必败. 分析: grundy值计算法的入门题. 代码: //poj 2960 //sep9 #include <iostream> #include <set> using namespace std; int s[128]; int grundy[10024]; int maxx; int num; int get_grundy(int x) { if(grundy[x]!=-1

POJ 2311 Cutting Game (sg函数)

给出一个N*M的纸片,每一次可以把一部分剪成两部分,谁剪出1*1的就赢了. http://poj.org/problem?id=2311 对于任何一个人,都不会先剪出1*n或者n*1,应该这样就必败了. 那我们考虑一个状态的后继中,最小的边也是2,这样就可以避免之前的问题,也不需要考虑类似ANTI-SG. 一旦出现2*2,2*3,3*2,这些都成了终止状态,不论怎么剪都会出现1*n,或者n*1 还是考察SG函数 # include<stdio.h> # include<algorithm

codeforces 768 E 变形NIM博弈/手写sg函数

题意:给你n堆石头,在同一堆石头下不能取两次相同的数目,问能否后手胜 题解:设一堆石头最多能取k次不同的石头数目,有nim博弈可以知道只要每一堆石头能取的次数异或起来为0则为必败局,则YES #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <map> #include <queue> #include <ve

[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律

[题目]题目链接 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more int