POJ - 2975 Nim (博弈)

题意:就是nim博弈,之后问你有几种赢的方法。

思路:nim博弈赢得方法就是他们的异或和为0嘛,我们先把这些石子都异或一遍之后得到一个值ans,之后异或a[i],这步操作的意思就是我抵消了a[i]操作嘛,看看我们要不要拿第i堆石子,如果 ans^a[i] < a[i] ,就说明我拿了当前这堆石子之后,我们可以把这堆石子拿走把他变成一个异或和为0的状态(因为a[i] 大 ,所以总可以把他变成一个异或和为0)那么我们就++就好了,上代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn = 1e3+10;
int a[maxn];
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		if(n == 0 )break;
		int te;
		int SG = 0;
		for(int i = 0 ; i < n ; i++)
		{
			scanf("%d",&a[i]);
			SG ^= a[i];
		}
		int ans = 0;
		for(int i = 0 ; i < n ; i++)
		{
			if((a[i] ^ SG) < a[i]) ans ++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wjmwsgj/article/details/80196506