poj 2975

题意:
就是nim游戏,但是问你先手如果可以必胜,有多少种走法。

输入:

3								//三个堆
7 11 13							//三堆分别是多少
2								//两个堆
1000000000 1000000000			//两堆分别是多少
0								//表示输入结束

输出

3			//三种走法
0			//表示先手必败

思路:
首先,所有堆的异或结果不为0的情况,先手必胜。其他则先手必败。
假设a0 ^ a1 ^ …^an-1=k,对于每个堆,想要必胜,就要把k变成0。每个堆的石头数的二进制位要么0要么1,所以说对于每个堆的必胜走法最多1种(或者选择此堆不能必胜),下面问题就转化成了判断移动某一个堆能不能必胜。
假设我们要判断在石头数量a0堆中移除一些石头能不能必胜,那么假设a1 ^ a2 ^ …^an-1=m,则k = a0 ^ m。如果能把a0变成m,那么k = a0 ^ m = m ^ m = 0。这时k就等于0。所以说只需要判断a0是否大于m就行了,又因为k ^ a0 = m ^ a0 ^ a0=m(对于k = a0 ^ m,两边同时异或a0),即k ^ a0 < a0;

下面是代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <vector>
#include <map>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long

using namespace std;

int n, a[1111],sum,ans;

int main(){
	while (scanf("%d", &n), n) {
		ans = sum = 0;
		for (int i = 0; i < n; i++)
			scanf("%d", a+i),sum^=a[i];
		for (int i = 0; i < n; i++)
			if ((sum ^ a[i]) < a[i])ans++;
		printf("%d\n",ans);
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值