题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=4994
Description
Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap. ---Wikipedia
Today, Nim takes revenge on you. The rule of the game has changed a little: the player must remove the objects from the current head(first) heap. Only the current head heap is empty can the player start to remove from the new head heap. As usual, the player who takes the last object wins.
Output
For each test case, output “Yes” if the first player can always win, otherwise “No”.
Sample Output
Yes
No
Mean:
给你n堆东西,两个人博弈的去拿,每次最少一个,最多是一堆,必须按顺序那,
也就是只有把第一堆的东西拿完了才能去拿第二堆东西,谁先拿完谁胜,问先手是否能胜利。
analyse:
显然是博弈,既然是博弈那么我们首先要干的事就是找必胜状态(或者必败状态),
1>对于任意一组数据(先假设里面没有某一堆里面的个数是1的时候)先手肯定是必胜的,因为先手可以每一次都只给这一堆留一个,让后手去拿这一个,到最后一堆的时候一下子全部拿走,
2>现在把有1的情况加进去,如果在开始就遇到连续的1的话就有可能失去主动权,只有开头连续个1的个数是偶数的时候最后主动权才还是自己的,
3>中间部分有1的情况不用考虑,我们可以先手可以通过全拿完或者给后手留一个来调节自己的必胜状态
4>还有一个就是全是1的时候记得特判一下。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=500009;
const int INF=0x3f3f3f3f;
const int mod=2009;
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, num;
scanf("%d", &n);
int sign=0;
for(int i=1; i<=n; i++)
{
scanf("%d", &num);
if(num!=1 && !sign)
sign=i;
}
///!sign&&n%2判断如果每一堆都是1,n为奇数赢
///sign&&(sign-1)%2==0开头连续个1的个数是偶数才会赢,同时也包括没有1的情况
if((!sign&&n%2) || (sign&&(sign-1)%2==0))
puts("Yes");
else
puts("No");
}
return 0;
}