HDU 5795 A Simple Nim (博弈 打表找规律)
A Simple Nim
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5795
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.
Source
2016 Multi-University Training Contest 6
##题意:
两人以最优策略对n堆物品进行操作,不能操作者输.
1. 从同一堆中取任意个(不为零).
2. 把一堆分成任意三堆(任一堆非空).
##题解:
变形的Nim博弈. 这里推导部分sg值找规律:
sg[0] = 0;
sg[1] = 1;
sg[2] = mex{sg[0], sg[1]} = mex{0,1} = 2;
sg[3] = mex{sg[0], sg[1], sg[2], sg[1,1,1]} = mex{0,1,2, sg[1]^sg[1]^sg[1] = 1} = 3;
sg[4] = 4;
sg[5] = 5;
sg[6] = 6;
sg[7] = 8;
sg[8] = 7;
sg[9] = 9;
综上,可以看出规律:
sg[0] = 0;
sg[8*k+7] = 8*k+8;
sg[8*k+8] = 8*k+7;
最后异或起来就可以了.
主要是之前做过类似版本:操作2是把一堆分成两堆.
HDU-3032 Nim or not Nim? (http://acm.hust.edu.cn/vjudge/contest/102108#problem/E)
这个版本的结果是sg[4k+3]=4k+4; sg[4k+4]=4k+3;
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 2100
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n;
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t;
while(scanf("%d", &n) != EOF)
{
LL ans = 0;
for(int i=1; i<=n; i++) {
LL x; scanf("%I64d", &x);
if(x % 8LL == 0LL) ans ^= x - 1;
else if(x % 8LL == 7LL) ans ^= x + 1;
else ans ^= x;
}
if(ans) puts("First player wins.");
else puts("Second player wins.");
}
return 0;
}
HDU 5795 A Simple Nim (博弈 打表找规律)的更多相关文章
- HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu 5795 A Simple Nim 博弈sg函数
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pro ...
- A Simple Nim (SG打表找规律)
题意:有n堆石子,每次可以将其中一堆分为数量不为0的3堆,或者从其中一堆中拿走若干个,最终拿完的那个人赢. 思路:直接暴力SG状态,然后找出其中的规律,异或一下每一堆的状态就可以了. #include ...
- HDU 5795 A Simple Nim(简单Nim)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- HDU 5795 A Simple Nim(SG打表找规律)
SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- HDU 5795 A Simple Nim ——(Nim博弈 + 打表)
题意:在nim游戏的规则上再增加了一条,即可以将任意一堆分为三堆都不为0的子堆也视为一次操作. 分析:打表找sg值的规律即可. 感想:又学会了一种新的方法,以后看到sg值找不出规律的,就打表即可~ 打 ...
- HDU 5795 A Simple Nim
打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
随机推荐
- SH1B LMR62014XMFE/NOPB
制造商National Semiconductor (TI) RoHS 输出电压20 V 输出电流1.4 A 输入电压2.7 V to 14 V 开关频率1.6 MHz 最大工作温度+ 85 C 安装 ...
- cdev_init函数
linux-2.6.22/include/linux/cdev.hstruct cdev { struct kobject kobj; // 每个 cdev 都是一个 kobje ...
- [HDOJ1827]Summer Holiday(强连通分量,缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1827 缩点后统计入度和当前强连通分量中最小花费,然后记录入度为0的点的个数和花费和就行了. /* ━━ ...
- C++ STL之查找算法
C++STL有好几种查找算法,但是他们的用法上有很多共同的地方: 1.除了binary_search的返回值是bool之外(查找的了返回true,否则返回false),其他所有的查找算法返回值都是一个 ...
- Kaleidoscope for mac
mac下的对比工具Kaleidoscope,是一款不错的对比工具,界面被广大用户所喜爱. window下使用beyond compare 3,具体设置步骤,请见:http://www.cnblogs. ...
- 图形数据库、NOSQL和Neo4j
简介 在众多不同的数据模型里,关系数据模型自80年代就处于统治地位,而且有不少实现,如Oracle.MySQL和MSSQL,它们也被称为关系数据库管理系统(RDBMS).然而,最近随着关系数据库使用案 ...
- vs2010 js代码折叠
方法一:插件 在Visaul Studio 2010中写js或css代码,缺少像写C#代码时的那种折叠功能,当代码比较多时,就很不方便. 但是已经有VS2010扩展支持这个功能,它就是--JSEn ...
- 完全二叉树的高度为什么是对lgN向下取整
完全二叉树的高度为什么是对lgN向下取整呢? 说明一下这里的高度:只有根节点的树高度是0. 设一棵完全二叉树节点个数为N,高度为h.所以总节点个数N满足以下不等式: 1 + 21 + 22 +……+ ...
- 使用源代码安装lnmp
一.安装nginx前,安装pcre. # tar zxvf pcre-8.12.tar.gz# ./configure# make# make install 二.安装nginx # tar zxvf ...
- Oracle 课程八之性能优化之10046事件
Oracle 的事件很多. 具体参考blog: Oracle 跟踪事件 set event 转摘:http://blog.csdn.net/tianlesoftware/archive/2009/12 ...