题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5795

A Simple Nim

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
> Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.
#### 输入
> Intput contains multiple test cases. The first line is an integer 1≤T≤100, the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n−1], representing heaps with s[0],s[1],...,s[n−1] objects respectively.(1≤n≤106,1≤s[i]≤109)
#### 输出
> For each test case,output a line whick contains either"First player wins."or"Second player wins".
#### 样例
> **sample input**
> 2
> 2
> 4 4
> 3
> 1 2 4
>
> **sample output**
> Second player wins.
> First player wins.

题解

打表找规律,发现只有%8的位置的sg值与之前的一个数交换了,其它的sg值都等于它本身。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
const int maxn = 111; int SG[maxn],vis[maxn+10]; void get_SG() {
SG[0] = 0;
SG[1] = 1;
SG[2] = 2;
for (int i = 3; i < maxn; i++) {
memset(vis, 0, sizeof(vis));
for (int j = 0; j < i; j++) {
vis[SG[j]] = 1;
}
for (int a = 1; a < i; a++) {
for (int b = a; a + b < i; b++) {
int c = i - a - b;
vis[SG[a] ^ SG[b] ^ SG[c]] = 1;
}
}
for (int j = 0;; j++) if (vis[j] == 0) {
SG[i] = j; break;
}
/*printf("%d\n", SG[i]);*/
if (SG[i] != i) printf("%d:%d\n", i,SG[i]);
}
} int main() {
//get_SG();
int tc,n;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
int ans = 0;
for (int i = 0; i < n; i++) {
int x; scanf("%d", &x);
if (x % 8==7) ans^=(x+1);
else if (x % 8 == 0) ans^=(x-1);
else ans ^= x;
}
if (!ans) puts("Second player wins.");
else puts("First player wins.");
}
return 0;
}

HDU 5795 博弈的更多相关文章

  1. HDU 5795 A Simple Nim(简单Nim)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  2. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  3. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

  4. HDU 5795 A Simple Nim (博弈 打表找规律)

    A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...

  5. HDU 5795:A Simple Nim(博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description   Two players take t ...

  6. HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  7. hdu 5795 A Simple Nim 博弈sg函数

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Pro ...

  8. HDU 5795 A Simple Nim ——(Nim博弈 + 打表)

    题意:在nim游戏的规则上再增加了一条,即可以将任意一堆分为三堆都不为0的子堆也视为一次操作. 分析:打表找sg值的规律即可. 感想:又学会了一种新的方法,以后看到sg值找不出规律的,就打表即可~ 打 ...

  9. HDU 5996 博弈

    http://acm.hdu.edu.cn/showproblem.php?pid=5996 博弈论待补. 这题变化了一下,因为注意到奇数层的东西(层数从1开始),对手可以模仿地动,那就相当于没动. ...

随机推荐

  1. js实现文字字幕滚动

    <div class="dggd_r" id="h" style="height:400px;overflow:hidden;display:i ...

  2. HBase从hdfs导入数据

    需求:将HDFS上的文件中的数据导入到hbase中 实现上面的需求也有两种办法,一种是自定义mr,一种是使用hbase提供好的import工具 一.hdfs中的数据是这样的 每一行的数据是这样的id ...

  3. php 安装redis扩展

    大家可以去http://code.google.com/p/redis/downloads/list这个地址找最近的下载wget http://redis.googlecode.com/files/r ...

  4. Ubuntu10.10的网络配置

    有一阵子着实对Ubuntu的网络配置很迷惑,耐下心来仔细上网找了找,有点小心得,总结一下. 先说下大概的配置过程,再去细究一些情况. 一.配置大概分三类:通过配置文件配置.通过命令配置.通过图形化的网 ...

  5. Enum枚举类型的使用笔记

    好处: 1.可以直接使用switch 2.可以实现toString()方法 笔记: 1.枚举类头部定义的成员变量,可以看做是枚举类的一个实例 public enum Color { RED(" ...

  6. C++多态性的浅析

    多态性是C++的一个重要特性,[不扯淡直接进入正题] 灵活运用多态,首先得知道类之间的继承.  当B继承了A类后,一般都是公有继承.  B的实例化对象的内存空间结构若是了解 就可以合理利用多态了. A ...

  7. 一幅图证明chrome的由来和目的

  8. 使用Moses中tokenizer.perl无法正常工作:纠结的"<" 和">"(已解决)

    发现居然没有输入文本和输出文本,折腾了一晚上,到了半夜终于搞懂了: 官方的Manual上这么写的: The tokenisation can be run as follows: ~/mosesdec ...

  9. VC中实现GCC的2个比较常用的位运算函数

    在GCC中内嵌了两个位运算的函数,但在VC中并没有这两个函数(有相似函数). //返回前导的0的个数. int __builtin_clz (unsigned int x) //返回后面的0个个数,和 ...

  10. ListView的多布局中的小问题

    今天用到了ListView的多布局,我们需要额外重写两个方法 //返回多布局的个数 @Override public int getViewTypeCount() { return 3; } //用该 ...