hdu4994Revenge of Nim(博弈)

题目链接:

选我选我

就是有n堆物品,然后只有一堆物品完全选完后,才能选好后面的,看最后谁赢。

思路:

确定第一个不为1的是轮到谁,如果谁得到了这个优势,那么他就必胜。。因为他可以人为控制后面的顺序了。。

题意:

题目:

Revenge of Nim

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 259    Accepted Submission(s): 119

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

BestCoder Round #9

Recommend

heyang   |   We have carefully selected several similar problems for you:  4996 4995 4992 4991 4990

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

const int maxn=1000+10;

int a[maxn];

int main()
{
    int t,n,cnt,ok;
    scanf("%d",&t);
    while(t--)
    {
        cnt=ok=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
        {
            if(a[i]==1)
                cnt++;
            else
            {
                ok=1;
                break;
            }
        }
        if(ok)
        {
           if(cnt%2)
              puts("No");
           else
              puts("Yes");
        }
        else
        {
            if(cnt%2)
              puts("Yes");
            else
             puts("No");
        }
    }
    return 0;
}

时间: 2024-10-10 04:11:38

hdu4994Revenge of Nim(博弈)的相关文章

HDU4994Revenge of Nim(博弈)

题目:HDU4994Revenge of Nim(博弈) 题目大意:有两个人在玩取石子的游戏,每个人只能从最前面的那堆取石子,至少取一个,最后取完石子的人win.问先手是否能获胜. 解题思路:每堆只有一个石子的话,那么这个人只能取完这一堆,然后当某一堆有超过1个石子的话,那么最先取这堆的人就可以根据后面的情况来调控这个石子(将这堆全取完,或者是取得只剩一个).所以这题先手是否胜利就要看前面连续一个情况了(最后一队不要考虑进去).奇数个1那么先手败,否则胜利. 代码: #include <cstd

HDU4994-Revenge of Nim(博弈)

题目链接 题意:有n堆石子,每个堆有Ai个石头,求先手是否能取得最后一个石头. 思路:这题主要在于前置1的多少能改变谁能成为先手,当前置1为偶数时,先手还是原来的先手,当为奇数时,先手就变成后手,后手变成先手.记得考虑当所有石堆都只有一个石头的情况. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ty

ACM学习历程—HDU 3915 Game(Nim博弈 &amp;&amp; xor高斯消元)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所有xor和为0. 那么自然变成了n个数里面取出一些数,使得xor和为0,求取法数. 首先由xor高斯消元得到一组向量基,但是这些向量基是无法表示0的. 所以要表示0,必须有若干0来表示,所以n-row就是消元结束后0的个数,那么2^(n-row)就是能组成0的种数. 对n==row特判一下. 代码:

UVA 11859 Division Game (Nim博弈)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32746 题意:有一个n*m(1<=n,m<=50)矩阵,每个元素均为2~10000之间的正整数,两个游戏者轮流操作.每次可以选一行中的1个或者大于1的整数,把他们中的每个数都变成它的某个真因子,比如12可以边长1,2,3,4或者6,不能操作的输. 分析:考虑每个数包含的素因子个数(比如12=2*2*3包含3个素因子),则让一个数"变成它的素因子"

hdu 5011 (nim博弈模版)

//nim博弈 //有n堆石头,两人轮流每次从一堆中拿至少1,之多全部的石头,没有石头可拿为lose //判断先手是win还是lose # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int main() { int n,i; __int64 a,sum; while(~scanf("%d",&n)) { sum=0; fo

HDU 1849 Rabbit and Grass(nim博弈)

题目地址:HDU 1849 初次接触nim博弈,感觉好神奇的说...居然可以跟异或运算扯上关系....给人类的智商跪了...作为地球人我感到很自豪.. 具体证明什么的看这篇博客被.传送门 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #inclu

HDU 1907 Nim博弈变形

1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形.设糖果数为1的叫孤独堆,糖果数大于1的叫充裕堆,设状态S0:a1^a2^..an!=0&&充裕堆=0,则先手必败(奇数个为1的堆,先手必败).S1:充裕堆=1,则先手必胜(若剩下的n-1个孤独堆个数为奇数个,那么将那个充裕堆全部拿掉,否则将那个充裕堆拿得只剩一个,这样的话先手必胜).T0:a1

UVA 1559 - Nim(博弈dp)

UVA 1559 - Nim 题目链接 题意:一开始有s个石子,2n个人轮流取石子,每个人有个最大能取数目,2n个人奇数一队,偶数一队,取到最后一个石子的队输,问谁赢 思路:记忆化搜索,每个人取的时候对应的后继状态如果有一个必败态,则该状态为必胜态,如果都是必胜态,则该状态为必败态 代码: #include <stdio.h> #include <string.h> int n, s, m[25], dp[25][10005]; int dfs(int now, int state

ZOJ 3529 A Game Between Alice and Bob (分解质因数+Nim博弈)

A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and Bob play the following game. A series of numbers is written on the blackboard. Alice and Bob take turns choosing one of the numbers, and replace it with one of

[Nim博弈]hdu 1850 Being a Good Boy in Spring Festival

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1850 Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4053    Accepted Submission(s): 2394 Problem Description