题解报告:hdu 1848 Fibonacci again and again(尼姆博弈)
Problem Description
F(1)=1;
F(2)=2;
F(n)=F(n-1)+F(n-2)(n>=3);
所以,1,2,3,5,8,13……就是菲波那契数列。
在HDOJ上有不少相关的题目,比如1005 Fibonacci again就是曾经的浙江省赛题。
今天,又一个关于Fibonacci的题目出现了,它是一个小游戏,定义如下:
1、 这是一个二人游戏;
2、 一共有3堆石子,数量分别是m, n, p个;
3、 两人轮流走;
4、 每走一步可以选择任意一堆石子,然后取走f个;
5、 f只能是菲波那契数列中的元素(即每次只能取1,2,3,5,8…等数量);
6、 最先取光所有石子的人为胜者;
假设双方都使用最优策略,请判断先手的人会赢还是后手的人会赢。
Input
m=n=p=0则表示输入结束。
Output
Sample Input
Sample Output
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int m,n,p,f[],sg[maxn];
bool vis[maxn];
void init(){
f[]=;f[]=;
for(int i=;i<;++i)f[i]=f[i-]+f[i-];//f数组长度为15即下标为14,因为fib[15]刚好大于1000
}
void get_SG(){
memset(sg,,sizeof(sg));
for(int i=;i<maxn;++i){
memset(vis,false,sizeof(vis));
for(int j=;j< && f[j]<=i;++j)//j<15要放在判断条件前面,不然会越界出错
vis[sg[i-f[j]]]=true;
for(int j=;j<maxn;++j)
if(!vis[j]){sg[i]=j;break;}
}
}
int main()
{
init();
get_SG();
while(cin>>m>>n>>p && (m+n+p)){
if(sg[m]^sg[n]^sg[p])cout<<"Fibo"<<endl;//如果三者异或不为0,则先手必赢
else cout<<"Nacci"<<endl;//否则后手必赢
}
return ;
}
题解报告:hdu 1848 Fibonacci again and again(尼姆博弈)的更多相关文章
- HDU 1848 Fibonacci again and again
题解:尼姆博弈,对于1至1000计算SG函数,每次取最小的前继值,SG值异或为0则为P-position. #include <cstdio> #include <cstring&g ...
- hdu-------(1848)Fibonacci again and again(sg函数版的尼姆博弈)
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- hdu 1849(Rabbit and Grass) 尼姆博弈
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4315 Climbing the Hill (阶梯博弈转尼姆博弈)
Climbing the Hill Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Su ...
- hdu 1849 (尼姆博弈)
http://acm.hdu.edu.cn/showproblem.php? pid=1849 简单的尼姆博弈: 代码例如以下: #include <iostream> #include ...
- hdu 1907 (尼姆博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1907 Problem Description Little John is playing very ...
- HDU.1850 being a good boy in spring festival (博弈论 尼姆博弈)
HDU.1850 Being a Good Boy in Spring Festival (博弈论 尼姆博弈) 题意分析 简单的nim 博弈 博弈论快速入门 代码总览 #include <bit ...
- POJ 3480 & HDU 1907 John(尼姆博弈变形)
题目链接: PKU:http://poj.org/problem? id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1907 Descri ...
- hdu 4315 Climbing the Hill && poj 1704 Georgia and Bob阶梯博弈--尼姆博弈
参考博客 先讲一下Georgia and Bob: 题意: 给你一排球的位置(全部在x轴上操作),你要把他们都移动到0位置,每次至少走一步且不能超过他前面(下标小)的那个球,谁不能操作谁就输了 题解: ...
- hdu 1848 Fibonacci again and again(SG函数)
Fibonacci again and again HDU - 1848 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)= ...
随机推荐
- Python ---- KMP(博文推荐+代码)
既解决完后宫问题(八皇后问题)后,又利用半天的时间完成了著名的“看毛片”算法——KMP.对于初学者来说这绝对是个大坑,非常难以理解. 在此,向提出KMP算法的三位大佬表示诚挚的敬意.!!!牛X!!! ...
- 1. Python中的基本数据类型、运算、变量
本文利用的是Python 3.x版本,建议学习3.x版本 Python中的基本数据类型.运算.变量 1. 基本数据类型 1.1 整数 py可以处理任意大小的整数,例如123,1234567891324 ...
- 8.1.1 Connection 对象
Connect是sqllite3模块中最基本的也是最重要的一个类,其主要方法如下表所示: 方法 说明 execute(sql[,parameters]) 执行一条SQL语句 executemany(s ...
- random随机库
random库是用于产生并运用随机数的标准库 主要包含的有9个随机函数,分别是: seed(), random(), randint(), getrandbits(), randrange(), un ...
- 倍增/线段树维护树的直径 hdu5993/2016icpc青岛L
题意: 给一棵树,每次询问删掉两条边,问剩下的三棵树的最大直径 点10W,询问10W,询问相互独立 Solution: 考虑线段树/倍增维护树的直径 考虑一个点集的区间 [l, r] 而我们知道了有 ...
- Markdown 基本使用
My First Markdown 标签(空格分隔): 未分类 # Markdown 11种基本语法## 设置标题在此输入正文标题设置(让字体变大,和word的标题意思一样)在Markdown当中设置 ...
- cogs 167. [USACO Mar07] 月度花费
167. [USACO Mar07] 月度花费 ★★ 输入文件:expense.in 输出文件:expense.out 简单对比时间限制:1 s 内存限制:128 MB Farmer ...
- Javascript控制回车键进行表单(form)提交(转)
一.采用钩子事件去捕获 键盘事件有3个: keydown,keypress,keyup分别是按下,按着没上抬,上抬键盘 . $(document).keyup(function(event){ if( ...
- Spring Boot使用thymeleaf模板时报异常:template might not exist or might not be accessible by any of the configured Template Resolvers
错误如下: template might not exist or might not be accessible by any of the configured Template Resolver ...
- HDOJ 5417 Victor and Machine 水
Victor and Machine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Othe ...