HDU 5794 - A Simple Nim
题意:
n堆石子,先拿光就赢,操作分为两种:
1.任意一堆中拿走任意颗石子
2.将任意一堆分成三小堆 ( 每堆至少一颗 )
分析:
答案为每一堆的SG函数值异或和.
故先打表寻找单堆SG函数规律.
其中,若 x 可分为 三堆 a,b,c ,则 SG[x] 可转移至子状态 SG[a] ^ SG[b] ^ SG[c] (三堆SG值异或和)
打表后发现:
SG[ 8*k - 1 ] = 8*k
SG[ 8*k ] = 8*k - 1
其余 SG[x] = x;
则可直接得出答案
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = ;
int T, n;
int main()
{
scanf("%d", &T);
while ( T-- )
{
scanf("%d", &n);
int sg = ;
for (int i = ; i <= n; i++)
{
int x; scanf("%d", &x);
if (x % == ) sg ^= (x - ) ;
else if ( (x + ) % == ) sg ^= (x + ) ;
else sg ^= x;
}
if(sg) puts("First player wins.");
else puts("Second player wins.");
}
}
/*
SG打表
*/
#include <iostream>
#include <cstring>
using namespace std;
int sg[];
int GetSG(int x)
{
if (sg[x] != -) return sg[x];
int vis[];
memset(vis, , sizeof(vis));
for (int i = ;i < x; i++)
vis[GetSG(i) ] = ;
int a,b,c;
for(a = ; a <= x; a++)
for(b = a; b <= x - a; b++)
for(c = b; c <= x - a - b; c++)
if(a+b+c == x)
vis[GetSG(a) ^ GetSG(b) ^ GetSG(c)] = ;
for(int i = ;; i++)
if(!vis[i]) return sg[x] = i;
}
int main()
{
memset(sg, -, sizeof(sg));
sg[] = ;
for (int i = ; i <= ; i++)
if(sg[i] == -) GetSG(i);
for(int i = ; i <= ; i++)
cout<<i<<" "<<sg[i]<<endl;
}
HDU 5794 - A Simple Nim的更多相关文章
- HDU 5795 A Simple Nim(简单Nim)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5794 - A Simple Chess
HDU 5794 - A Simple Chess题意: 马(象棋)初始位置在(1,1), 现在要走到(n,m), 问有几种走法 棋盘上有r个障碍物, 该位置不能走, 并规定只能走右下方 数据范围: ...
- HDU 5795 A Simple Nim (博弈 打表找规律)
A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...
- HDU 5794 A Simple Chess dp+Lucas
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 A Simple Chess Time Limit: 2000/1000 MS (Java/O ...
- 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 (博弈) ---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 ...
- HDU 5794 A Simple Chess (容斥+DP+Lucas)
A Simple Chess 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5794 Description There is a n×m board ...
- HDU 5794 A Simple Chess (Lucas + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题. 题目让你求一个棋子开始在( ...
随机推荐
- 原来真的不会用指针[*p++]
Describe: 有2字节字符数据,需要转换成2字节的短整型,字符数据低字节在前. Analyse: 其实就是取一下数据,移位再或一下就好了,大伙都这样想的. Ex1: 假设tmp1就是短整型,p指 ...
- codeforces 339C Xenia and Weights(dp或暴搜)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Xenia and Weights Xenia has a set of weig ...
- 【转】Tomcat集群Cluster实现原理剖析
此文章来源:http://zyycaesar.iteye.com/blog/296606 此文章作者:zyycaesar 对于WEB应用集群的技术实现而言,最大的难点就是如何能在集群中的多个节点之间保 ...
- 推荐的 CSS 书写顺序
//显示属性 display list-style position float clear //自身属性 width height margin padding border background ...
- http cookie
一.cookie的大小 cookie只能存储最大4kb的数据.cookie的名/值中的值不允许包含分号.逗号和空白符.因此可以采用encodeURIComponent()编码,读取的时候先采用deco ...
- DDMS files not found:hprof-conv.exe的解决办法
或者是you must restart adb and eclipse这类错误 原因:一般是豌豆荚之类的软件影响的,所以,以后要慎用了. 解决方案:先找一下在sdk\tools目录下是否有hprof- ...
- 如何提升app开发效率
无论在什么行业,用户永远都是不可替代的“上帝”,一切的服务,开发都得按照用户的意愿来进行.然而在app开发领域中,专业的技术操作却并不像逛街淘货一样清晰可见,更多的需要app开发人员一行行代码敲出来, ...
- ubuntu 12.04(Precise Pangolin)启用休眠(Hibernate)功能的方案
官方支持文档:https://help.ubuntu.com/12.04/ubuntu-help/power-hibernate.html 按照这篇官方支持,因为休眠功能存在bug,12.04没有默认 ...
- 八款强大的jQuery图片滑块动画插件
jQuery是一款相当轻巧的JavaScript框架,目前几乎每一个WEB项目都在使用jQuery,因为jQuery插件实在太丰富,尤其是 一些图片滑块插件和jQuery焦点图插件,更是多如牛毛,很多 ...
- 剑指offer-面试题2.实例Singleton模式
题目:设计一个类,我们只能生成该类的一个实例 这道题显然是对设计模式的考察,很明显是单例模式.什么是单例模式呢,就是就像题目所说的只能生成一 个类的实例.那么我们不难考虑到下面几点: 1.不能new多 ...