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. 查找一个数组中最小的前n项

    /****************************************************************** find the biggest x number in a s ...

  2. LibRTMP优化之调整输出块大小

    1. 为什么要调整输出块大小 首先在RTMP_Connect0函数中LibRTMP是关闭了Nagle算法这个TCP选项的,为了实时性这样做是好的,但是要注意到LibRTMP的结构体RTMP的成员是有m ...

  3. Oracle Day07 PL/SQL基础

    1.基本格式 set serveroutput on declare -- 申明部分 name ); begin -- 执行的sql语句 ; dbms_output.put_line(name); e ...

  4. JPA使用的HelloWorld

    一.使用JPA持久化对象的步骤 •创建 persistence.xml, 在这个文件中配置持久化单元 –需要指定跟哪个数据库进行交互; –需要指定 JPA 使用哪个持久化的框架以及配置该框架的基本属性 ...

  5. VC++中使用ADO方式操作ACCESS数据库

    ADO(ActiveX Data Object)是Microsoft数据库应用程序开发的新接口,是建立在OLE DB之上的高层数据库访问技术,即使你对OLE DB,COM不了解也能轻松对付ADO,因为 ...

  6. Eclipse配置--智能补全

    定位到:Windows→Preferences→Java→Editor→Content Assist 将Auto Activation triggers for java的默认值“.”修改为" ...

  7. lua学习

    在lua中,一切都是变量,除了关键字. 1.注释: 单行注释: 连续两个减号“--”表示注释的开始,一直延续到行末.相当于C语言中的“//” 多行注释:由“--[[”表示注释开始, “]]”表示注释结 ...

  8. 关于微信分享功能开发的一些bug

    wx.onMenuShareTimeline({//onMenuShareTimeline title: (h('#mainForm').children('.content').inf('value ...

  9. 获得正在编辑行的数据 esayui datagrid

    function getEditRow(datagridId) {//datagridId为table容器的id var input = $('#' + datagridId).parent().fi ...

  10. 命名空间“Microsoft”中不存在类型或命名空间名称“Office”(是缺少程序集引用吗?)

    通过引用这个解决,不同的的office版本,中间的版本号可能不同,如图所示: