UVA - 1482 Playing With Stones

Description

Download as PDF

You and your friend are playing a game in which you and your friend take turns removing stones from piles. Initially there areN 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$ \le$100) denoting the number of testcases. Each testcase begins with an integerN (1$ \le$N$ \le$100) the number of piles. The next line contains N integersa1, a2, a3,...,aN (1$ \le$ai$ \le$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个。两个游戏者轮流操作,每次可以选一堆,拿走至少一个石子,但不能拿走超过一半的石子
思路:组合游戏和的问题,SG函数的应用,由于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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码中有几个问题: 1. while 循环的判断条件应该是 `stones.length > 1`,因为只有至少有两个石头时才需要进行破碎的操作。所以应该将 `while( stones.length==1 || stones.length==0 )` 修改为 `while( stones.length > 1 )`。 2. 在交换石头的位置时,应该使用临时变量来保存一个石头的值,然后再进行交换。所以应该将 `a=stones[j];` 修改为 `int temp = stones[i];`,将 `stones[i]=stones[j];` 修改为 `stones[i]=stones[j];`,将 `stones[j]=a;` 修改为 `stones[j]=temp;`。 3. 在判断最后剩下的石头重量时,应该使用索引 `stones.length-1` 和 `stones.length-2` 来获取最后两个石头的重量。所以应该将 `if(stones[stones.length-1]<=stones[stones.length])` 修改为 `if(stones[stones.length-1]<=stones[stones.length-2])`,将 `stones.length=stones.length-2;` 修改为 `stones.length -= 2;`。 4. 最后返回的应该是 `stones[0]`,而不是 `stones[stones.length]`。所以应该将 `return stones[stones.length];` 修改为 `return stones[0];`。 修正后的代码如下: ```java class Solution { public int lastStoneWeight(int[] stones) { while (stones.length > 1) { for (int i = 0; i < stones.length; i++) { for (int j = 1; j < stones.length; j++) { if (stones[i] > stones[j]) { int temp = stones[i]; stones[i] = stones[j]; stones[j] = temp; } } } if (stones[stones.length - 1] <= stones[stones.length - 2]) { stones[stones.length - 2] -= stones[stones.length - 1]; stones = Arrays.copyOf(stones, stones.length - 1); } else { stones = Arrays.copyOf(stones, stones.length - 2); } } return stones[0]; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值