题目链接:

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

A Simple Nim

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#### 问题描述
> 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.
#### 输入
> 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)
#### 输出
> 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.

题解

打表找规律,发现只有%8的位置的sg值与之前的一个数交换了,其它的sg值都等于它本身。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
const int maxn = 111; int SG[maxn],vis[maxn+10]; void get_SG() {
SG[0] = 0;
SG[1] = 1;
SG[2] = 2;
for (int i = 3; i < maxn; i++) {
memset(vis, 0, sizeof(vis));
for (int j = 0; j < i; j++) {
vis[SG[j]] = 1;
}
for (int a = 1; a < i; a++) {
for (int b = a; a + b < i; b++) {
int c = i - a - b;
vis[SG[a] ^ SG[b] ^ SG[c]] = 1;
}
}
for (int j = 0;; j++) if (vis[j] == 0) {
SG[i] = j; break;
}
/*printf("%d\n", SG[i]);*/
if (SG[i] != i) printf("%d:%d\n", i,SG[i]);
}
} int main() {
//get_SG();
int tc,n;
scanf("%d", &tc);
while (tc--) {
scanf("%d", &n);
int ans = 0;
for (int i = 0; i < n; i++) {
int x; scanf("%d", &x);
if (x % 8==7) ans^=(x+1);
else if (x % 8 == 0) ans^=(x-1);
else ans ^= x;
}
if (!ans) puts("Second player wins.");
else puts("First player wins.");
}
return 0;
}

HDU 5795 博弈的更多相关文章

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

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

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

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

  3. S-Nim HDU 1536 博弈 sg函数

    S-Nim HDU 1536 博弈 sg函数 题意 首先输入K,表示一个集合的大小,之后输入集合,表示对于这对石子只能去这个集合中的元素的个数,之后输入 一个m表示接下来对于这个集合要进行m次询问,之 ...

  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(博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description   Two players take t ...

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

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

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

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

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

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

  9. HDU 5996 博弈

    http://acm.hdu.edu.cn/showproblem.php?pid=5996 博弈论待补. 这题变化了一下,因为注意到奇数层的东西(层数从1开始),对手可以模仿地动,那就相当于没动. ...

随机推荐

  1. 如何使用CSS3画出一个叮当猫

    刚学习了这个案例,然后觉得比较好玩,就练习了一下.然后发现其实也不难,如果你经常使用PS或者Flash的话,应该就会知道画个叮当猫是很容易 的事,至少我是这么觉得.但是,用CSS3画出来确实是第一次接 ...

  2. JavaScript模块化---AMD规范

    JavaSript模块化 在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发?     模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问 题进行系 ...

  3. POJ C++程序设计 编程作业—类和对象 编程题#3

    编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序的输出 ...

  4. 刚开始学IOS遇到的类和方法

    框架:Core FoundationCFGetRetainCount. 类:NSRunLoop.NSAutoreleasePool.NSStringFormClass.UIApplicationMai ...

  5. 《第一行代码--Android》阅读笔记之数据持久化

    1.升级数据库 为了避免手工清空数据(或卸载重装APP),重写SQLiteOpenHelper里面的onUpgrade()方法   引用自http://blog.csdn.net/longvslove ...

  6. silverlight 退出系统(关闭当前网页),通过调用JS

    确认后直接退出系统,关闭当前页面 页面部分: <HyperlinkButton x:Name="LinkExit" Style="{StaticResource L ...

  7. 记一下ajax里get与post的异同

    以下均为个人笔记,如有错误,欢迎指正 get与post在写法上来讲基本上差不多 $.ajax({ type:"get",//get data:data, success:funct ...

  8. (转)Android属性设置android:noHistory="true"

    设置 android:noHistory="true"后,该Activity在statck中不留历史痕迹.默认的值是false. 举例说明,假设有三个Activity分别是:A,B ...

  9. asp.net 发布后用IP访问正常,用机器名访问布局出错

    问题如题[发布后IP访问正常,用机器名访问布局出现问题] 出现此问题的原因:IE文档模型发生变化,比如ip访问时IE文档模型为:IE10,换用机器名访问时,文档模型变为IE7 解决方法:设置默认IE版 ...

  10. uC/OS-II之系统函数20160526

    任务管理 1 OSTaskCreate() 建立一个新任务.任务的建立可以在多任务环境启动之前,也可以在正在运行的任务中建立.中断处理程序中不能 建立任务.一个任务可以为无限循环的结构. 函数原型:I ...