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. WHERE谓词对索引使用的影响

    本篇博文只测试WHERE谓词对multi-column index使用的影响,主要篇幅是SQL代码+截图.详细内容请参考<Inside the SQL Server Query Optimize ...

  2. SVG 参考手册

    1. SVG元素模块 Animation.Module animate animateColor animateTransform animateMotion set mpath 剪裁模块 clipP ...

  3. UIPickerView详解

    一.UIPickerView 1.UIPickerView的常见属性 // 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id< ...

  4. html中 让 ul 的多个 li 在一行内显示

    有时候会需要ul的li都在同一行显示,避免li跑到下一行去,今天遇到了这个问题,记录下来,避免忘记~ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

  5. Fixed 鸟粪一样的TreeView下的NodeMouseDoubleClick Bug

    private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { Rectangl ...

  6. 微信开发---------jsapi_ticket拿不到的问题。

    今天代码没动,出现的问题是分享朋友圈,朋友,分享到QQ,我设置的分享链接都失效了.找来找去也不知道什么原因,没办法就只有一个个打印参数并查看文档,先把jssdk页面上的debug设置为true(微信原 ...

  7. JSon_零基础_003_将Map集合对象转换为JSon格式的对象字符串,返回给界面

    将Map集合对象转换为JSon格式的对象字符串,返回给界面 需导入的jar包: 编写servlet: package com.west.webcourse.servlet; import java.i ...

  8. php session 跨子域和跨服务器解决方式

    Session主要分两部分:    一个是Session数据,该数据默认情况下是存放在服务器的tmp文件下的,是以文件形式存在     另一个是标志着Session数据的Session Id,Sess ...

  9. paper 90:人脸检测研究2015最新进展

    搜集整理了2004~2015性能最好的人脸检测的部分资料,欢迎交流和补充相关资料. 1:人脸检测性能 1.1 人脸检测测评 目前有两个比较大的人脸测评网站: 1:Face Detection Data ...

  10. paper 74:MATLAB图像处理_HSV与RGB颜色空间互转

    HSV空间:分别是H(色调)——S(饱和度)——V(亮度) 与HSI颜色空间类似:分别是H(色调)——S(饱和度)——I(强度) 注意: 强度和亮度差不多是一个概念. 饱和度代表的是渗入白光的数量级, ...