HDU5795-A Simple Nim(SG函数)

本文介绍了一种名为ASimpleNim的游戏,玩家通过取石子进行对决。文章提供了问题描述、输入输出样例及解题思路。核心在于发现特定规律以确定胜者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Simple Nim

                                                                  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                          Total Submission(s): 1464    Accepted Submission(s): 790


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
  
2 2 4 4 3 1 2 4
 

Sample Output
  
Second player wins. First player wins.
 

Author
UESTC
 

Source
 

Recommend
wange2014
 


题意:取石子游戏,n堆石头,有两种操作方式,一、在一堆中取任意颗石子(大于0)。二、将一堆分成三堆,每堆数量大于0。取到最后一块石子的人获得胜利。
解题思路:先小数据打表求sg值,可以发现sg值的规律。当i%8==7时,其sg值为i+1,当i%8==0时,其sg值为i-1(sg[0]=0)。



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int sg[100];
bool vis[100];

int main()
{
	//打表
	/*sg[0] = 0;
	for (int i = 1; i <= 50; i++)
	{
		memset(vis, 0, sizeof(vis));
		for (int j = 0; j < i; j++) vis[sg[j]] = 1;
		for (int j = 1; j < i; j++)
		{
			for (int k = 1; k < j; k++)
			{
				int u = i - j - k;
				if (u > 0)
				{
					int tmp = sg[j] ^ sg[k] ^ sg[u];
					vis[tmp] = 1;
				}
				else break;
			}
		}
		for (int j = 0;; j++)
			if (!vis[j])
			{
				sg[i] = j;
				printf("sg[%d]: %d\n", i, x);
				break;
			}
	}*/
	int t, n;
	scanf("%d", &t);
	while (t--)
	{
		int ans = 0, a;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a);
			if (a % 8 == 7) ans ^= (a + 1);
			else if (a % 8 == 0) ans ^= (a - 1);
			else ans ^= a;
		}
		if (ans) printf("First player wins.\n");
		else printf("Second player wins.\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值