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

A Simple Nim

Problem Description
 
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.
 
Input
 
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)
 
Output
 
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.
 
题意:和普通的Nim相似以外,还有一个操作,就是可以将一堆数量大于三的石头分成三份,每份可以不同(进行该操作的话该回合是不能取的)。
 #include <cstdio>
#include <cstring>
#define N 1000005
int sg[N];
/*
官方题解:
sg[0]=0 当x=8k+7时sg[x]=8k+8, 当x=8k+8时sg[x]=8k+7, 其余时候sg[x]=x;(k>=0) 打表找规律可得,数学归纳法可证。
*/
void sg_do()
{
//学习打表找规律
sg[] = ;
bool vis[N];
for(int i = ; i <= ; i++) {
memset(vis, , sizeof(vis));
for(int j = ; j <= i; j++) {
vis[sg[i-j]] = ;
//拿走的情况
}
if(i >= ) {
//只要对分成的三部分取异或,就代表x所能转移到的下一状态。
for(int j = ; j <= i - ; j++) {
for(int k = ; k <= i - ; k++) {
if(j + k < i) {
vis[sg[i-j-k]^sg[j]^sg[k]] = ;
//将一堆拆成三份的情况
}
}
}
}
int j = ;
while(vis[j]) j++;
sg[i] = j;
printf("%d : %d\n", i, sg[i]);
}
} int main()
{
// sg_do();
int t;
scanf("%d", &t);
while(t--) {
int n, sg, x, ans = ;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &x);
if((x+) % == ) sg = x + ;
else if(x % == ) sg = x - ;
else sg = x;
ans ^= sg;
}
if(ans == ) puts("Second player wins.");
else puts("First player wins.");
}
return ;
}

HDU 5795:A Simple Nim(博弈)的更多相关文章

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

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

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

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

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

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

  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 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

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

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

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

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

  8. HDU 5795 A Simple Nim

    打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  10. HDU 5794 - A Simple Nim

    题意:    n堆石子,先拿光就赢,操作分为两种:        1.任意一堆中拿走任意颗石子        2.将任意一堆分成三小堆 ( 每堆至少一颗 )        分析:    答案为每一堆的 ...

随机推荐

  1. MongoDB的C#封装类

    代码: samus驱动 using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...

  2. for循环计数

    1.巧用for循环计数,将文件每10行写到另一个文件,每遍历一行i就加1 with open('/etc/passwd') as f1, open('/tmp/passwd','w') as f2: ...

  3. IPv4 地址分类

    IP地址==网络地址 + 主机地址 IP地址是由4个字节组成,一个字节为8位,所以一共是32位,以点分十进制表示,例如192.168.1.1 IP地址分类:1.A类地址: IP地址==网络地址 1个字 ...

  4. OSG第一个Demo

    环境:Vs2010 OpenSceneGraph-3.0.1-VS10.0.30319-x86-debug-12741 OpenSceneGraph-3.0.1-VS10.0.30319-x86-re ...

  5. Windows下MongoDB安装与PHP扩展

    MongoDB是什么就不再累述了,下面只写MongoDB安装与PHP扩展的方法. 一,安装准备 MongoDB 如果网速慢,可以到MongoDB中文社区的百度网盘下载,密码3gun.(根据你的操作系统 ...

  6. Git 使用的配置 常用命令

    老文一篇 搬过来 1. git的部分配置 # 全局提交用户名与邮箱 git config --global user.name "simon" git config --globa ...

  7. 试用windows Azure

    试用windows Azure, 需要国外手机注册,信用卡注册. windows操作系统,只有2008R2,2012,2012R2可以选择,我选择XS最低档,然后选2012R2,欧洲数据中心,那个慢啊 ...

  8. IntelliJ IDEA 项目相关的几个重要概念介绍

    必备材料介绍 IntelliJ IDEA 对其他 IDE 转过来的用户有特别优待,对其专门整理了非常棒的资料,还请其他 IDE 过来的用户抽时间查看,会有很大帮助:Eclipse 用户可以看:http ...

  9. 单链表的回文判断(O(n)时间复杂度和O(1)的空间复杂度)

    对于单链表来说,判断回文最简单的方法就是遍历链表,将链表中的元素复制到数组中,然后对数组进行判断是否是回文数组,但是这不符合O(1)的空间复杂度. 由于空间复杂度的要求,需要就地操作链表,不能开辟多余 ...

  10. Swift实战-豆瓣电台(七)显示动画

    youku观看地址http://v.youku.com/v_show/id_XNzMxODQzNDIw.html 这是一个很酷的动画效果.特别是数据多的时候 知识点 在单元格(Cell)显示方法中设置 ...