尼姆博弈论+sg打表找规律

当sg打表超时时,我们就要打出前n项然后找sg值的规律了。
给出两题:
一.hdu3032Nim or not Nim?
Nim or not Nim?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3811 Accepted Submission(s): 1964

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
2009 Multi-University Training Contest 13 - Host by HIT

把某一堆石子分成两堆,当分不了就失败,下一题是三堆
将一个游戏状态拆分为3个小游戏,这三个状态就是它的后继状态
AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
//把某一堆分三堆,分不了就失败
int sg[100005]; 
int s[1000005];
map<int,int> p;
void getsg(int n)//n太大了全部打出来 要找规律 
{
	fill(sg,sg+1+n,0); 
	for(int i=1;i<=n;i++){
		p.clear();
		for(int j=0;i-j>=0;j++)
		p[sg[i-j]]=1;//可以取任意步长 
		//再将石子分成三堆 
		for(int j=1;j<=i;j++)
			for(int k=1;k<=i;k++)//分两堆堆,逐一枚举i的所有后继状态 
				if(j+k==i)//一种合法状态 
				{
					p[sg[j]^sg[k]]=1;
				}
		for(int j=0;;j++)//mex运算
		if(!p[j])
		{
			sg[i]=j;
			break;
		}
		printf("sg[%d]:%d\n",i,sg[i]);//输出来找规律 
	}
	return;
}//Time Limit Exceeded

int main(void)
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int x=0;
		/*
		getsg(n);*/
		//打表找规律发现 
		//当i是4的倍数时sg[i]=i-1
		//当i%4==3时 sg[i]=i+1
		//否则为 i
		for(int i=1;i<=n;i++){
			scanf("%d",&s[i]);
			if(s[i]%4==3)
			x^=(s[i]+1);
			else if(s[i]%4==0)
			x^=(s[i]-1);
			else
			x^=s[i];
		}
		//for(int i=1;i<=n;i++) x^=sg[s[i]];
		if(!x)//奇异局 先手必败 
		printf("Bob\n");
		else
		printf("Alice\n"); 
	}
	return 0;
}

二.hdu5789 A Simple Nim
A Simple Nim
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2012 Accepted Submission(s): 1046

Problem Description
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 interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.

Input
Intput 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≤106,1≤s[i]≤109)

Output
For each test case,output a line whick contains either"First player wins.“or"Second player wins”.

Sample Input

2
2
4 4
3
1 2 4

Sample Output

Second player wins.
First player wins.

Author
UESTC

Source
2016 Multi-University Training Contest 6

AC代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
//把某一堆分三堆,分不了就失败
int sg[100005]; 
int s[1000005];
map<int,int> p;
void getsg(int n)//n太大了全部打出来 要找规律 
{
	fill(sg,sg+1+n,0); 
	for(int i=1;i<=n;i++){
		p.clear();
		for(int j=0;i-j>=0;j++)
		p[sg[i-j]]=1;//可以取任意步长 
		//再将石子分成三堆 
		for(int j=1;j<=i;j++)
			for(int k=1;k<i;k++)
				for(int o=1;(o+k+j)<=i;o++)//分三堆,逐一枚举i的所有后继状态 
					if(j+k+o==i)//一种合法状态 
					{
						p[sg[j]^sg[k]^sg[o]]=1;
					}
		for(int j=0;;j++)//mex运算
		if(!p[j])
		{
			sg[i]=j;
			break;
		}
		printf("sg[%d]:%d\n",i,sg[i]);//输出来找规律 
	}
	return;
}//Time Limit Exceeded

int main(void)
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int x=0;
		/*
		getsg(n);*/
		//打表找规律发现 
		//当i是8的倍数时sg[i]=i-1
		//当i%8=7时 sg[i]=i+1
		//否则为 i
		for(int i=1;i<=n;i++){
			scanf("%d",&s[i]);
			if(s[i]%8==0)
			x^=(s[i]-1);
			else if(s[i]%8==7)
			x^=(s[i]+1);
			else
			x^=s[i];
		}
		//for(int i=1;i<=n;i++) x^=sg[s[i]];
		if(!x)//奇异局 先手必败 
		printf("Second player wins.\n");
		else
		printf("First player wins.\n"); 
	}
	return 0;
}
发布了68 篇原创文章 · 获赞 15 · 访问量 9009

猜你喜欢

转载自blog.csdn.net/qq_43791377/article/details/103540321