POJ-2234 Matches Game(尼姆博弈)

来源:互联网 发布:js双组份防水涂料 编辑:程序博客网 时间:2024/05/21 22:54

题目链接

POJ-2234 Matches Game

题目大意

n堆石子,每次可以从任意一堆拿任意数量的石子,至少拿1个,两人轮流进行,问先手是否必胜?

Sample Input

2 45 45
3 3 6 9

Sample Output

No
Yes

思路

直接是:尼姆博弈。
好多都只是给出结果,并没有给出尼姆博弈为什么可以使用异或运算。
这个解释证明了异或和为0是必败点。

代码

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,ans,a;int main() {    while(scanf("%d",&n)==1) {        ans=0;        while(n-->0) {            scanf("%d",&a);            ans^=a;        }        printf("%s\n",ans==0?"No":"Yes");    }    return 0;}
0 0