Playing With Stones--UVALive5059石子游戏(超级石子堆的SG找规律)

UVALive - 5059 

题目链接https://vjudge.net/contest/321177#problem/A

You and your friend are playing a game in which you and your friend take turns removing stones from piles. Initially there are N piles with a1, a2, a3, . . . , aN number of stones. On each turn, a player must remove at least one stone from one pile but no more than half of the number of stones in that pile. The player who cannot make any moves is considered lost. For example, if there are three piles with 5, 1 and 2 stones, then the player can take 1 or 2 stones from first pile, no stone from second pile, and only 1 stone from third pile. Note that the player cannot take any stones from the second pile as 1 is more than half of 1 (the size of that pile). Assume that you and your friend play optimally and you play first, determine whether you have a winning move. You are said to have a winning move if after making that move, you can eventually win no matter what your friend does.

Input

The first line of input contains an integer T (T ≤ 100) denoting the number of testcases. Each testcase begins with an integer N (1 ≤ N ≤ 100) the number of piles. The next line contains N integers a1, a2, a3, . . . , aN (1 ≤ ai ≤ 2 ∗ 1e18) the number of stones in each pile.

Output

For each testcase, print ‘YES’ (without quote) if you have a winning move, or ‘NO’ (without quote) if you don’t have a winning move.

Sample Input

4

2

4 4

3

1 2 3

3

2 4 6

3

1 2 1

Sample Output

NO

YES

NO

YES


题目大意:有n堆石子,每次选择一堆,拿走至少一个,但不能拿走超过一半的石子,谁不能拿谁输。

。。。。看起来很简单,一个SG过去就没了。。。但看看数据范围1e18。。。。SG写不了,这个时候就只能看看规律了,俗话说博弈先打表嘛。。。

我们先用SG函数打个表,将石子数量少的SG值求出来,比如先将前石子个数为1到30的表打出来:

SG();
for (int i = 1; i <= 30; i++) {
	printf("%d ", sg[i]);
*******
*******
void SG()
{
	memset(sg, 0, sizeof(sg));
	for (int i = 1; i <= 40; i++) {
		memset(s, 0, sizeof s);
		for (int j = 1; j * 2 <= i; j++)
			s[sg[i - j]] = 1;
		for (int j = 0;; j++)
			if (!s[j]) { sg[i] = j; break; }
	}
}

最后就会出现这样一个东西:0 1 0 2 1 3 0 4 2 5 1 6 3 7 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15这个序列。

咋一看好像没什么规律,但对于每个偶数来讲,它的SG值就是x/2,删去偶数,那么你会得到的序列:

0 1 0 2 1 3 0 4 2 5 1 6 3 7 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15。。。。和原序列一样的。。。那么也就可以表示成如果该堆的石子数为奇数,则它的SG值为SG(x/2),如果x/2还是奇数的话就继续下去,直到它为偶数,最后返回x/2就好了。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int mod = 1e6;
const int mac = 1e4 + 10;
const int inf = 1e8 + 10;
int f[105];
int sg[mac], s[mac];
void SG();
ll getsg(ll x) {
	return x % 2 == 0 ? x / 2 : getsg(x / 2);
}
int main()
{
	SG();
	/*for (int i = 1; i <= 30; i++) {
		printf("%d ", sg[i]);
	}*/
	int t, n;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		ll x;
		ll v = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%lld", &x);
			v ^= getsg(x);
		}
		if (v) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}
void SG()
{
	memset(sg, 0, sizeof(sg));
	for (int i = 1; i <= 40; i++) {
		memset(s, 0, sizeof s);
		for (int j = 1; j * 2 <= i; j++)
			s[sg[i - j]] = 1;
		for (int j = 0;; j++)
			if (!s[j]) { sg[i] = j; break; }
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值