码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 1482 Playing With Stones

时间:2014-08-17 11:46:42      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   os   io   for   ar   问题   

Description

bubuko.com,布布扣

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 (Tbubuko.com,布布扣100) denoting the number of testcases. Each testcase begins with an integer N (1bubuko.com,布布扣Nbubuko.com,布布扣100) the number of piles. The next line contains N integers a1, a2, a3,..., aN (1bubuko.com,布布扣aibubuko.com,布布扣2 * 1018) 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堆石头,分别有a1,a2...an个。两个游戏者轮流操作,每次可以选一堆,拿走至少一个石子,但不能拿走超过一半的石子
思路:组合游戏和的问题,由于ai的范围太大,我们尝试先写递推程序,找规律,打出表后,发现当n为偶数的时候,SG(n)=n/2,n为奇数的时候,SG(n)=SG(n/2)
打表程序:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100;

int SG[maxn], vis[maxn];

int main() {
	SG[1] = 0;
	for (int i = 2; i <= 30; i++) {
		memset(vis, 0, sizeof(vis));
		for (int j = 1; j*2 <= i; j++)
			vis[SG[i-j]] = 1;
		for (int j = 0; ; j++) 
			if (!vis[j]) {
				SG[i] = j;
				break;
			}
		printf("%d ", SG[i]);
	}
	printf("\n");
	return 0;
}
正解:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;

ll SG(ll x) {
	return x&1?SG(x/2):x/2;
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n;
		ll a, v = 0;
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			scanf("%lld", &a);	
			v ^= SG(a);
		}
		if (v)
			printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


UVA - 1482 Playing With Stones,布布扣,bubuko.com

UVA - 1482 Playing With Stones

标签:des   style   http   os   io   for   ar   问题   

原文地址:http://blog.csdn.net/u011345136/article/details/38637473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!