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. python_如何建立包

    步骤: (1)包的名称为drawing (2)drawing中建立模块color和shape 视图: 备注: (1) E:/python_script/已经加入到系统变量path中 (2) 建立包时, ...

  2. [PCL]3 欧式距离分类EuclideanClusterExtraction

    EuclideanClusterExtraction这个名字起的很奇怪,欧式距离聚类这个该如何理解?欧式距离只是一种距离测度的方法呀!有了一个Cluster在里面,我以为是某一种聚类算法,层次聚类?k ...

  3. ios网络_json数据解析

    网络上数据传输以json或者xml格式. json是字典 或者 数组 或者字典跟数组嵌套的形式.解析json就是把json反序列化(解析)---把json转换为oc对象.json序列化就是把oc对象转 ...

  4. linux:习惯

    1>.系统里面必须最少有两个账号,一个root,一个普通账户; 2>.平时登录系统均使用普通账户登录,除非需要配置或者执行特殊命令才切换至root; 3>.linux系统下严格区分大 ...

  5. C++Primer 第十二章

    //1.标准库提供了两种智能指针类型来管理动态对象,均定义在头文件memory中,声明在std命名空间. // shared_ptr:允许多个指针指向同一个对象. // unique_ptr:独占所指 ...

  6. BenchmarkSQL测试脚本实现

    sqlTableCreates DROP SCHEMA IF EXISTS benchmarksql CASCADE; CREATE SCHEMA benchmarksql; create table ...

  7. MySQL单表最大限制

    想把一个项目的数据库导出来,然后倒入到自己熟悉的MySQL数据库中进行运行和调试.导出来后,发现sql文件整整有12G多大,忽然想起来,MySQL好像有个叫做容量限制的神奇特性,但是忘了上限是多少了, ...

  8. 转:python webdriver API 之定位一组对象

    webdriver 可以很方便的使用 find_element 方法来定位某个特定的对象,不过有时候我们却需要定位一组对象,WebElement 接口同样提供了定位一组元素的方法 find_eleme ...

  9. 【Origin】时迁念昔

    -清明,未曾归,恰大门来沪,晨起准备,车道劳顿,隔五年而见一面,感时事变迁,物各两异,小道旁之争艳花木,觉道长且阻,叹而留记. 清明时节雨纷纷, 不辞跋涉见故人; 红花绿叶皆失色, 握手言欢语无伦. ...

  10. [转]Chrome 控制台console的用法

    Chrome 控制台console的用法 下面我们来看看console里面具体提供了哪些方法可以供我们平时调试时使用. 目前控制台方法和属性有: ["$$", "$x&q ...