Fibonacci again and again

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 1848
64-bit integer IO format: %I64d      Java class name: Main

 
任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:
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

输入数据包含多个测试用例,每个测试用例占一行,包含3个整数m,n,p(1<=m,n,p<=1000)。
m=n=p=0则表示输入结束。

 

Output

如果先手的人能赢,请输出“Fibo”,否则请输出“Nacci”,每个实例的输出占一行。

 

Sample Input

1 1 1
1 4 1
0 0 0

Sample Output

Fibo
Nacci

Source

 
解题:SG函数
 

对于一个给定的有向无环图,定义关于图的每个顶点的 Sprague-Grundy 函数g如下:g(x)=mex{ g(y) | y是x的后继 },这里的g(x)即sg[x]

例如:取石子问题,有1堆n个的石子,每次只能取{1,3,4}个石子,先取完石子者胜利,那么各个数的SG值为多少?

sg[0]=0,f[]={1,3,4},

x=1时,可以取走1-f{1}个石子,剩余{0}个,mex{sg[0]}={0},故sg[1]=1;

x=2时,可以取走2-f{1}个石子,剩余{1}个,mex{sg[1]}={1},故sg[2]=0;

x=3时,可以取走3-f{1,3}个石子,剩余{2,0}个,mex{sg[2],sg[0]}={0,0},故sg[3]=1;

x=4时,可以取走4-f{1,3,4}个石子,剩余{3,1,0}个,mex{sg[3],sg[1],sg[0]}={1,1,0},故sg[4]=2;

x=5时,可以取走5-f{1,3,4}个石子,剩余{4,2,1}个,mex{sg[4],sg[2],sg[1]}={2,0,1},故sg[5]=3;

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int f[maxn] = {,},sg[maxn];
bool vis[maxn];
void init(){
int i,j;
for(i = ; f[i-] <= && i <= ; i++)
f[i] = f[i-]+f[i-];
memset(sg,,sizeof(sg));
for(i = ; i <= ; i++){
memset(vis,false,sizeof(vis));
for(j = ; f[j] <= i; j++)
vis[sg[i-f[j]]] = true;
for(j = ; j <= ; j++)
if(!vis[j]){
sg[i] = j;
break;
}
}
}
int main() {
init();
int a,b,c;
while(scanf("%d %d %d",&a,&b,&c),a||b||c){
if(sg[a]^sg[b]^sg[c]) puts("Fibo");
else puts("Nacci");
}
return ;
}

最新文章

  1. 使用EDMX查询(EF基础系列15)
  2. ubuntu的一些操作
  3. Python初学者需要注意的问题
  4. vs c++ 将string转换为double
  5. Codeigniter CRUD代码快速构建
  6. 使用Docker搭建consul集群+registrator实现服务自动注册。
  7. lintcode :搜索旋转排序数组
  8. iOS开发:iOS的整体架构以及API介绍
  9. Java 文件名操作的相关工具类
  10. preg_replace($pattern, $replacement, $content) 修饰符的奇葩作用
  11. encodeURI和encodeURIComponent的比较
  12. Jquery获对HTML控件的控制
  13. web前端简介
  14. HDU1205 吃糖果
  15. GO开发[六]:golang反射(reflect)
  16. C++结束进程 并能显示其父进程
  17. Error: client: etcd cluster is unavailable or misconfigured; error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused
  18. PAT-A1004. Counting Leaves (30)
  19. 【IDE】我的花里胡哨VS
  20. HTML命名规范

热门文章

  1. [POI2007]天然气管道Gaz
  2. 配置Ubuntu16.04第02步:更改镜像源,更新系统
  3. js中toFixed重写
  4. 关于tomcat一些简介
  5. 转】SparkSQL中的内置函数
  6. MVC C# 直接导出txt文件
  7. .net 字符串和JSON格式的互换
  8. Oracle分区表例子
  9. git 恢复误删的文件
  10. Fragment懒加载预加载