hdu 4994(博弈)

Revenge of Nim

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 562    Accepted Submission(s): 290


Problem 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
 

 

Source
 
题意:按照顺序取完n堆石子,每次取一个或者一堆。
关键点:看出取到第一个>1的石子堆的人是必胜态。
/**
对于某个人来说,只要他能够取到第一个>1的堆,那么他就可以决定后面的人的状态了,所以第一个取到>1的堆的人
是赢家。所以我们只要讨论前面有多少==1的堆就行了。
2.如果里面存在>1的堆,那么在第一个>1的堆前面有偶数个=1的堆先手才会赢
1.如果没有>1的堆,那么奇数个=1的堆先手就会赢。
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;

int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        int n,cnt=0,v;
        bool flag = false;
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&v);
            if(v>1&&!flag)
            {
                flag = true;
                cnt = i-1;
            }
        }
        if(!flag) cnt = n;
        if(flag){
            if(cnt%2==1) printf("No\n");
            else printf("Yes\n");
        }else{
            if(cnt%2==0) printf("No\n");
            else printf("Yes\n");
        }
    }
    return 0;
}

 

posted @ 2016-07-05 10:16  樱花庄的龙之介大人  阅读(148)  评论(0编辑  收藏  举报