题意:
就是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;
}

1919

被折叠的 条评论
为什么被折叠?



