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

Sample Output
Yes
No

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4994

************************************************

题意:有n堆石子,每堆有ai个,每次按照堆的先后顺序至少取走一个石子,最后取完石子的人赢,如果第一个取石子的人赢则输出Yes,否则,输出No。

分析:一开始老是从大于1的上面去判断,后来又琢磨琢磨,才发现只需要对第一个大于1的堆之前等于1的个数来判断就可以了。

如果每堆石子的数目都大于1,则first在取前n-1堆石子时,每次都留下一个石子,在取最后一堆时,一次取完,则first必赢;

但是如果有些堆的石子数不大于1时,当每出现一个大于1的石子堆时,对于取该堆石子的人,他有两种取法,一是全部取走,二是取走ai-1个(即留下1个),而这两种方式在不同的情况中可以使用其中一种使得自己更容易赢。

因此只要判断出从前往后的前n-1堆中,谁先取得第一堆数目大于1的石子堆的主动权,谁就是赢者。

所以只需要求出第一堆数目大于1的石子堆前有多少个1就可以判断了,如果有偶数个1, 则Yes,如果有奇数个1,则No。

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std; #define N 1200
#define INF 0x3f3f3f3f int a[N]; int main()
{
int T,n,i; scanf("%d", &T); while(T--)
{
scanf("%d", &n); int ans=;
for(i=;i<n;i++)
scanf("%d", &a[i]); for(i=;i<n-;i++)///前n-1堆中第一个大于1的堆前的1的个数
if(a[i]==)
ans++;
else///不为1时要break
break; if(ans%==)
printf("Yes\n");
else
printf("No\n");
}
return ;
}

HDU - 4994 Revenge of Nim (取石子游戏)的更多相关文章

  1. 51nod1069【Nim取石子游戏】

    具体看:萌新笔记之Nim取石子游戏可以这么写: #include <bits/stdc++.h> using namespace std; typedef long long LL; in ...

  2. 萌新笔记之Nim取石子游戏

    以下笔记摘自计算机丛书组合数学,机械工业出版社. Nim取石子游戏 Nim(来自德语Nimm!,意为拿取)取石子游戏. 前言: 哇咔咔,让我们来追寻娱乐数学的组合数学起源! 游戏内容: 有两个玩家面对 ...

  3. HDU 2516 (Fabonacci Nim) 取石子游戏

    这道题的结论就是,石子的个数为斐波那契数列某一项的时候,先手必败:否则,先手必胜. 结论很简单,但是证明却不是特别容易.找了好几篇博客,发现不一样的也就两篇,但是这两篇给的证明感觉证得不清不楚的,没看 ...

  4. poj2368 Buttons Nim取石子游戏

    链接:http://poj.org/problem?id=2368 和前面差距还是很大啊囧 代码: k,a;main(i){,i=;i<=k/&&k%i;++i);k%i||(a ...

  5. HDU.2516 取石子游戏 (博弈论 斐波那契博弈)

    HDU.2516 取石子游戏 (博弈论 斐波那契博弈) 题意分析 简单的斐波那契博弈 博弈论快速入门 代码总览 #include <bits/stdc++.h> #define nmax ...

  6. 取石子游戏 HDU 1527 博弈论 威佐夫博弈

    取石子游戏 HDU 1527 博弈论 威佐夫博弈 题意 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两 ...

  7. HDU 2516 取石子游戏(斐波那契博弈)

    取石子游戏 Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...

  8. hdu 1527 取石子游戏(Wythoff Game)

    题意:Wythoff Game 思路:Wythoff Game #include<iostream> #include<stdio.h> #include<math.h& ...

  9. HDU 2516 取石子游戏(FIB博弈)

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. zonghe

      package hcxAction; import hcxMode.Advertises; import hcxMode.Areas; import hcxMode.Saveresume; imp ...

  2. hdu5573 二叉树找规律,二进制相关

    input T 1<=T<=100 n k 1<=n<=1e9  n<=2^k<=2^60 output 从1走到第k层,下一层的数是上一层的数×2或者×2+1,每 ...

  3. mac 文件路径问题

    Finder栏显示访问路径 操作步骤: 打开“终端”(应用程序->实用工具),输入以下两条命令: defaults write com.apple.finder _FXShowPosixPath ...

  4. winsock编程WSAEventSelect模型

    winsock编程WSAEventSelect模型 WSAEventSelect模型和WSAAsyncSelec模型类似,都是用调用WSAXXXXXSelec函数将socket和事件关联并注册到系统, ...

  5. do循环的100米自由落体

    #include "stdio.h" void main() { float h=100.0,sum=100.0; ; do { sum=sum+h; h=h/; g++; }); ...

  6. 关于个人网站选择虚拟主机还是VPS服务器的讨论

    还记得当初才开始学习建站的时候,选择的第一款虚拟主机是全HTML的主机,那时候的虚拟主机还分为HTML或者是ASP,PHP的都很少,在国内接触的学习较多还是以ASP为主,PHP是最近几年才开始流行.如 ...

  7. android 线程池的使用

    转自http://www.trinea.cn/android/java-android-thread-pool/ Java(Android)线程池 介绍new Thread的弊端及Java四种线程池的 ...

  8. 【伪一周小结(没错我一周就做了这么点微小的工作)】HDOJ-1241 Oil Deposits 初次AC粗糙版对比代码框架重构版

    2016 11月最后一周 这一周复习了一下目前大概了解的唯一算法--深度优先搜索算法(DFS).关于各种细节的处理还是极为不熟练,根据题意判断是否还原标记也无法轻松得出结论.不得不说,距离一个准ACM ...

  9. 在Windows上安装Python

    首先,从官网下载 最新版本 的Python 2.7.可通过 Python官网 的”Windows Installer”链接保证下载到的版本是最新的. Windows版本是MSI文件格式,双击它即可开始 ...

  10. 谷歌浏览器web开发教程之开始篇:使用sublime

    你的代码编辑器是主要的开发工具:你使用它去编辑和保存代码段.你可以通过学习编辑器快捷键和以及安装关键插件来好而快的写出代码. 目录 安装sublime文本编辑器 为什么使用包管理器? 安装插件 摘要 ...