一般的nim博弈都是最后取完者算赢。而有些相反的恰是取完算输。
http://acm.hdu.edu.cn/showproblem.php?pid=1907
http://acm.hdu.edu.cn/showproblem.php?pid=2509
这题与以往的博弈题的胜负条件不同,谁先走完最后一步谁输,但他也是一类Nim游戏,即为anti-nim游戏。
首先给出结论:先手胜当且仅当 ①所有堆石子数都为1且游戏的SG值为0(即有偶数个孤单堆-每堆只有1个石子数);②存在某堆石子数大于1且游戏的SG值不为0.
证明:
- 若所有堆都为1且SG值为0,则共有偶数堆石子,故先手胜。
i)只有一堆石子数大于1时,我们总可以对该石子操作,使操作后堆数为奇数且所有堆的石子数均为1;ii)有超过一堆的石子数1时,先手将SG值变为0即可,且总还存在某堆石子数大于1
因为先手胜。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
int main()
{
int T,n,i,x;
while(~scanf("%d",&n))
{
int ans=0,t=0;
for (i=1;i<=n;i++)
{
scanf("%d",&x);
ans^=x;
if (x>1) t=1;
}
if (t==0)
{
if (n & 1) puts("No");
else puts("Yes");
}
else if (ans) puts("Yes");
else puts("No");
}
return 0;
}