A Simple Nim(博弈----Multi-SG打表找规律)

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  1T100, 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[n1], representing heaps with  s[0],s[1],...,s[n1] objects respectively. (1n106,1s[i]109)
 

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

Sample Input
 
 
224 431 2 4
 

Sample Output
 
 
Second player wins.First player wins.
 

题意:一堆糖果两人轮流进行操作

1 , 在一堆中取任意个糖果

2 , 将一堆糖果分成三堆(每堆非空)

分成三堆可以这样分 , 或者其他操作只要能使其相加等于该数即可

	for(int j = 1 ; j <= i ; j++)//分成三堆 
		{
			for(int k = 1 ; k <= i ; k++)
			{
				for(int o = 1 ; o <= i ; o++)
				{
					if(j + k + o == i)
					{
						Hash[sg[j] ^ sg[k] ^sg[o]] = 1;
					}
				}
			}
		 } 

仅仅是通过打表找个规律 , 再根据规律来写 , 所以这里不考虑时间复杂度。

SG打表代码:

int sg[maxn] , Hash[maxn];
void getsg(int x)
{
	memset(sg , 0 , sizeof(sg));
	for(int i = 0 ; i <= x ; i++)
	{
		memset(Hash , 0 , sizeof(Hash));
		for(int j = 1 ; j <= i ; j++)
		{
			Hash[sg[i-j]] = 1;//取任意数字 
		}
		for(int j = 1 ; j <= i ; j++)//分成三堆 
		{
			for(int k = 1 ; k <= i ; k++)
			{
				for(int o = 1 ; o <= i ; o++)
				{
					if(j + k + o == i)
					{
						Hash[sg[j] ^ sg[k] ^sg[o]] = 1;
					}
				}
			}
		 } 
		 for(int j = 0 ; j<= x ; j++)
		 {
		 	if(Hash[j] == 0)
		 	{
		 		sg[i] = j;
		 		break;
			 }
		 }
		 cout << i << ":" << sg[i] << endl;
	}
}

由此发现规律;

x%8 == 0 时 , sg[x] = x-1;

x%8 == 7 时 , sg[x] = x+1;

因此代码如下:

#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	int n , t , x , flag;
	while(~scanf("%d" , &n))
	{
		while(n--)
		{
			 flag = 0 ;
			scanf("%d" , &t);
			while(t--)
			{
				scanf("%d" , &x);
				if(x % 8 == 0)
				{
					flag ^= x-1;
				}
				else if(x%8 == 7)
				{
					flag ^= x+1;
				 } 
				 else
				 {
				 	flag ^= x;
				 }
			
			}
			if(flag == 0)
			{
				printf("Second player wins.\n");
			}
			else
			{
				printf("First player wins.\n");
			}
		}
	}
	return 0;
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值