UVA - 12333 Revenge of Fibonacci (大数 字典树)
The well-known Fibonacci sequence is defined as following:
F(0) = F(1) = 1
F(n) = F(n − 1) + F(n − 2) ∀n ≥ 2
Here we regard n as the index of the Fibonacci number F(n).
This sequence has been studied since the publication of Fibonacci’s book Liber Abaci. So far, many properties of this sequence have been introduced.
You had been interested in this sequence, while after reading lots of papers about it. You think there’s no need to research in it anymore because of the lack of its unrevealed properties. Yesterday, you decided to study some other sequences like Lucas sequence instead.
Fibonacci came into your dream last night. “Stupid human beings. Lots of important properties of Fibonacci sequence have not been studied by anyone, for example, from the Fibonacci number 347746739...”
You woke up and couldn’t remember the whole number except the first few digits Fibonacci told you. You decided to write a program to find this number out in order to continue your research on Fibonacci sequence.
Input
There are multiple test cases. The first line of input contains a single integer T denoting the number of test cases (T ≤ 50000).
For each test case, there is a single line containing one non-empty string made up of at most 40 digits. And there won’t be any unnecessary leading zeroes.
Output
For each test case, output the smallest index of the smallest Fibonacci number whose decimal notation begins with the given digits. If no Fibonacci number with index smaller than 100000 satisfy that condition, output ‘-1’ instead — you think what Fibonacci wants to told you beyonds your ability.
Sample Input
15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739 5610
Sample Output
Case #1:0
Case #2: 25
Case #3: 226
Case #4: 1628
Case #5: 49516
Case #6: 15
Case #7: 15
Case #8: 15
Case #9: 43764
Case #10: 49750
Case #11: 10
Case #12: 51
Case #13: -1
Case #14: 1233
Case #15: 22374
#include <bits/stdc++.h>
using namespace std;
struct Node{
int id;
Node * next[];
Node(){
id = -;
for(int i = ; i < ; ++i)
next[i] = NULL;
}
};
char Fib[], In[];
int F[][];
Node * const root = new Node();
void add_node(char *str, int id)
{
Node * u = root;
for(int i = , len = strlen(str); i < len && i <= ; ++i){
int v = str[i] - '';
if(!u->next[v])
u->next[v] = new Node();
u = u->next[v];
if(u->id == -)
u->id = id;
}
}
int query(char *str)
{
Node * u = root;
for(size_t i = , len = strlen(str); i < len; ++i){
u = u->next[str[i]-''];
if(!u) return -;
}
return u->id;
}
void init()
{
memset(F, , sizeof(F));
F[][] = F[][] = ;
int s = , e = ;
add_node((char *)"", );
add_node((char *)"", );
for(int i = ; i < ; ++i){
int p = i%, q = (i+)%;
for(int j = s; j < e; ++j)
F[p][j] = F[p][j] + F[q][j];
for(int j = s; j < e; ++j)
if(F[p][j]>=){
F[p][j] %= ;
F[p][j+] += ;
}
if(F[p][e]) ++e;
if(e - s > ) ++s;
int r = e - , cnt = ;
memset(Fib, , sizeof(Fib));
while(r >= && cnt<)
Fib[cnt++] = F[p][r--] + '';
add_node(Fib, i);
}
}
int main()
{
ios::sync_with_stdio(false);
init();
int T; cin >> T;
for(int i = ; i <= T; ++i){
cin >> In;
printf("Case #%d: %d\n", i, query(In));
}
return ;
}
UVA - 12333 Revenge of Fibonacci (大数 字典树)的更多相关文章
- UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树
题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...
- UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)
题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...
- UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...
- UVa 12333 Revenge of Fibonacci (字典树+大数)
题意:给定一个长度小于40的序列,问你那是Fib数列的哪一项的前缀. 析:首先用大数把Fib数列的前100000-1项算出来,注意,一定不能是100000,要不然会WA的,然后每个数取前40位,不足4 ...
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- UVA 11488 Hyper Prefix Sets (字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- uva 11488 - Hyper Prefix Sets(字典树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- TZOJ 3820 Revenge of Fibonacci(大数+trie)
描述 The well-known Fibonacci sequence is defined as following: Here we regard n as the index of the F ...
随机推荐
- 【二】、UML基础知识——图图解乾坤
[二].UML基础知识 UML概述 UML是一个通用的可视化建模语言,不同于编程语言,它通过一些标准的图形符号和文字来对系统进行建模.用于对软件进行描述.可视化处理.构建软件系统的文档.是一套总结了以 ...
- centos7安装Elasticsearch及Es-head插件详细教程(图文)
懒惰了很久,今天来写一下Elasticsearch在centos7上安装教程以及安装过程中可能出现的报错解决方式,有不对的地方,烦请各位看官多多指教! 一.ES安装 1.环境检查 确保有java环境, ...
- Elasticsearch之集群
ElasticSearch集群 ES集群是一个 P2P类型(使用 gossip 协议)的分布式系统,除了集群状态管理以外,其他所有的请求都可以发送到集群内任意一台节点上,这个节点可以自己找到需要转发给 ...
- RequestFacade对象获取请求头时忽略大小写
也许在Controller层 在RequestFacde文件中getHeader函数逻辑实现如下所示: public String getHeader(String name) { if(this.r ...
- 使用Ajax时[object%20object] 报错的解决方案
踩坑经过 最近初学Ajax,当我想把Ajax应用到自己项目中的时候,没有达到理想的效果,还报了如下错误: 点击图中报错,产生报错页面如下: 当时写的Ajax如下: // 提交修改密码表单 $(&quo ...
- NPM 包管理工具详解,使用教程
NPM 包管理工具 1.1 定义:什么是 NPM NPM 全称 Node Package Manager,它是 JavaScript 的包管理工具, 并且是 Node.js 平台的默认包管理工具.通过 ...
- elsearch搜索引擎 + painless脚本语言入门
最近项目用到了elsearch,ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎. 自从版本6.0之后,其默认脚本语言变为 painless . ...
- Wix 快速开发安装包程序 (二)安装行为
上一小节,主要介绍了构建最小级别的安装包,这个安装包所做的事情很简单,主要是打包好一些文件,然后放到用户机器的某个位置下面. 这个小节,主要是总结安装过程的各种行为如何使用Wix编写. 一.写注册表 ...
- 初始socket编程
服务端语法 import socket # 导入套接字模块# 生成一个socket对象进行网络编程操作server = socket.socket(family=socket.AF_INET, typ ...
- KMP刷题记录
[BZOJ4698][SDOI2008]Sandy的卡片 差分一下然后选一个串,用这个串的所有前缀和其他串kmp,求出最长的公共部分即可 代码: #include <bits/stdc++.h& ...