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. C# 整形、双精度浮点型、字符串与字节型的相互转化

    整形.双精度浮点型.字符串与字节型的相互转化,如下 using System; using System.Collections.Generic; using System.Linq; using S ...

  2. Linux 基本收集

    ifconfig eth0 192.168.1.223 切换到root账号开始是$符号输入su输入root密码转换成# 就变成了root账号 dr 查看盘符ls /etc/ 查看etc文件夹下面的文件 ...

  3. 浅谈EasyUI---C#三层架构---

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  4. 使用UDEV绑定ASM多路径磁盘

    OS版本:RHEL6.4 1) 找出目标磁盘的分区别名 # dmsetup ls |grep data data01 (253:9) data04 (253:4) data03 (253:3) dat ...

  5. 很赞的PHP字符串加密函数

    最近, 从discuz里面发现了一个很牛的加密解密函数. 此函数的厉害之处在于可以在指定时间内加密还原字符串,超时无法还原 这样我们就可以拿此函数来做很多用途了,比如:单点登录的token加密传输啦, ...

  6. Python学习总结5:数据类型及转换

    Python提供的基本数据类型主要有:整型.浮点型.字符串.列表.元组.集合.字典.布尔类型等等. Python可以用一些数据类型函数,直接进行转换: 函数                       ...

  7. struts请求基本类型参数接收

    01:导包,web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version ...

  8. 互联网分享知识(一转载 http://www.cnblogs.com/baochuan/p/4636103.html)

         风雪之隅-Laruence的博客 http://www.laruence.com/ PHP开发组成员, Zend兼职顾问, PHP7核心开发者, Yaf, Yar, Yac等项目作者.偏向P ...

  9. 夺命雷公狗—angularjs—6—单条数据的遍历

    我们在实际的工作中常常会处理到一些数据的遍历,这些数据都是后端传到前端的,有些公司会让前端帮忙处理一点遍历的工作,废话不多说,直接上代: <!doctype html> <html ...

  10. Deep learning的一些教程 (转载)

    几个不错的深度学习教程,基本都有视频和演讲稿.附两篇综述文章和一副漫画.还有一些以后补充. Jeff Dean 2013 @ Stanford http://i.stanford.edu/infose ...