HDU 4994 Revenge of Nim(组合游戏)

来源:互联网 发布:linux find 绝对路径 编辑:程序博客网 时间:2024/06/04 19:35
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
21221 1
 

Sample Output
YesNo题意:一次取n堆,每次可以取非0个,只有在这堆取完后才能再取下一堆,拿到最后一个的为胜,判断先手胜负思路:可以推出,只有在全为偶数的且都为1的时候,才能是后手胜,而前缀的1可以改变先后手的性质,因为有非1的话就是先手赢,所以这次再判断时候改变性质就行了
#include <iostream>  #include <cstdio>  #include <cstring>  #include <algorithm>  typedef __int64 ll;using namespace std;  const int maxn = 20005;  int main() {      int t, n;      scanf("%d", &t);      while (t--) {          scanf("%d", &n);          int flag = 1;        int cnt = 0;        ll a;        for (int i = 0; i < n; i++) {            scanf("%I64d", &a);            if (a == 1 && flag)                 cnt++;            else flag = 0;        }        if (flag) {            if (cnt & 1)                printf("Yes\n");            else printf("No\n");        }        else {            if (cnt & 1)                printf("No\n");            else printf("Yes\n");        }    }      return 0;  }  


0 0
原创粉丝点击