HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)
| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
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
1 4 1
0 0 0
Sample Output
Nacci
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
const int N = ;
const int M = ; int fib[];
int SG[N]; int mex(int x)
{
bool vis[M];
memset(vis,,sizeof(vis));
for(int i=;i<M;i++)
{
int t = x - fib[i];
if(t < ) break;
if(SG[t] == -)
SG[t] = mex(t);
vis[SG[t]] = ;
}
for(int i=;;i++)
if(!vis[i]) return i;
} void Init()
{
fib[] = ;
fib[] = ;
for(int i=;i<M;i++)
fib[i] = fib[i-] + fib[i-];
memset(SG,-,sizeof(SG));
for(int i=;i<N;i++)
SG[i] = mex(i);
} int main()
{
Init();
int a,b,c;
while(~scanf("%d%d%d",&a,&b,&c))
{
if(a == && b == && c == ) break;
int ans = ;
ans ^= SG[a];
ans ^= SG[b];
ans ^= SG[c];
if(ans) puts("Fibo");
else puts("Nacci");
}
return ;
}
非深搜:
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
const int N = ;
const int M = ;
int fib[];
int SG[N];
void get()
{
bool vis[M];
for(int i=;i<N;i++) //sg数组
{
memset(vis,,sizeof(vis));
for(int j=;j<M&&fib[j]<=i;j++) //要用的s数组 注意这里有等号
{
vis[SG[i-fib[j]]]=;
}
for(int x=;x<N;x++)
if(!vis[x])
{
SG[i]=x;
break;
}
} }
void Init()
{
fib[] = ;
fib[] = ;
for(int i=;i<M;i++)
fib[i] = fib[i-] + fib[i-];
memset(SG,,sizeof(SG)); //这里定义成 -1和0都可以
get();
} int main()
{
Init();
int a,b,c;
while(~scanf("%d%d%d",&a,&b,&c))
{
if(a == && b == && c == ) break;
int ans = ;
ans ^= SG[a];
ans ^= SG[b];
ans ^= SG[c];
if(ans) puts("Fibo");
else puts("Nacci");
}
return ;
}
HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)的更多相关文章
- HDU 2516 取石子游戏(斐波那契博弈)
取石子游戏 Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- HDU.2516 取石子游戏 (博弈论 斐波那契博弈)
HDU.2516 取石子游戏 (博弈论 斐波那契博弈) 题意分析 简单的斐波那契博弈 博弈论快速入门 代码总览 #include <bits/stdc++.h> #define nmax ...
- hdu 2516 取石子游戏 (斐波那契博弈)
题意:1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍. 取完者胜,先取者负输出"Second win",先取者胜 ...
- 题解报告:hdu 2516 取石子游戏(斐波那契博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2516 Problem Description 1堆石子有n个,两人轮流取.先取者第1次可以取任意多个, ...
- {HDU}{2516}{取石子游戏}{斐波那契博弈}
题意:给定一堆石子,每个人最多取前一个人取石子数的2被,最少取一个,最后取石子的为赢家,求赢家. 思路:斐波那契博弈,这个题的证明过程太精彩了! 一个重要的定理:任何正整数都可以表示为若干个不连续的斐 ...
- HDU 2516 取石子游戏 斐波纳契博弈
斐波纳契博弈: 有一堆个数为n的石子,游戏双方轮流取石子,满足: 1)先手不能在第一次把所有的石子取完: 2)之后每次可以取的石子数介于1到对手刚取的石子数的2倍之间(包含1和对手刚取的石子数的2倍) ...
- 博弈论基础知识: 巴什博奕+斐波那契博弈+威佐夫博奕+尼姆博弈(及Staircase)(转)
(一)巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个.最后取光者得胜.若(m+1) | n,则先手必败,否则先手必胜.显然,如果n=m+1 ...
- 51Nod 1070 Bash游戏 V4(斐波那契博弈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1070 题意: 思路: 这个是斐波那契博弈,http://blog.csd ...
- hdu2516斐波那契博弈
刚开始想用sg函数做,想了半天没一点思路啊. 原来这是一个新题型,斐波那契博弈 斐波那契博弈模型:有一堆个数为 n 的石子,游戏双方轮流取石子,满足:1. 先手不能在第一次把所有的石子取完:2. 之后 ...
随机推荐
- BZOJ2456 mode
Description 给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数. Input 第1行一个正整数n. 第2行n个正整数用空格隔开. Output 一行一个正整数 ...
- JAVA的面向对象编程--------课堂笔记
面向对象主要针对面向过程. 面向过程的基本单元是函数. 什么是对象:EVERYTHING IS OBJECT(万物皆对象) 所有的事物都有两个方面: 有什么(属性):用来描述对象. 能够做什么 ...
- MyEclipse------execute()使用方法
execute()方法应该仅在语句能返回多个ResultSet对象,多个更新计数或ResultSet对象与更新计数的组合时使用. testExecute.jsp <%@ page languag ...
- C#实现eval 进行四则运算
昨天在园子里看到有园友,写了相同标题的一篇文章.重点讲的是中缀表达式转换为后缀表达式的算法,但是实现的四则运算 有bug.其实我没看之前也不懂什么是 中缀和后缀表达式,之前有用过js eval 内置函 ...
- ios严格检验身份证号码有效性
+ (BOOL)checkIDCard:(NSString *)sPaperId { //判断位数 && sPaperId.length != ) { return NO; } NSS ...
- SQL注入攻击技巧总结
0×01 你要知道目前有哪些数据库 微软公司旗下的: Microsoft SQL server 简称 MS-SQL 或者 SQL SERVER (大型数据库操作,功能和性能异常强大)(一般也是ASP或 ...
- centos安装gitlab
原文链接: http://www.centoscn.com/image-text/install/2015/0320/4929.html http://www.01happy.com/centos-6 ...
- java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration解决方法
Autowiring of fields failed; nested exception is...........Error creating bean with name 'siteOperat ...
- git基础知识总结
1,clone git clone https://github.com/KoMiles/helloword helloword 2,pull git pull 3,commit git commit ...
- javascript中parentNode,childNodes,children的应用详解
本篇文章是对javascript中parentNode,childNodes,children的应用进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 "parentNode&qu ...