hdu---4994---Revenge of Nim

题目链接: 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.

Input

The first line contains a single integer T, indicating the number of test cases.  
Each test case begins with an integer N, indicating the number of heaps. Then N integer Ai follows, indicating the number of each heap successively, and the player must take objects in this order, from the first to the last. 
[Technical Specification]  1. 1 <= T <= 100  2. 1 <= N <= 1 000  3. 1 <= Ai <= 1 000 000 000

Output

For each test case, output “Yes” if the first player can always win, otherwise “No”.

Sample Input

2
1
2
2
1 1

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;
}

 

转载于:https://www.cnblogs.com/w-y-1/p/5796364.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值