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 (大数 字典树)的更多相关文章

  1. UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树

    题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...

  2. UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)

    题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...

  3. UVA 12333 Revenge of Fibonacci

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. HDU 4099 Revenge of Fibonacci(高精度+字典树)

    题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...

  5. UVa 12333 Revenge of Fibonacci (字典树+大数)

    题意:给定一个长度小于40的序列,问你那是Fib数列的哪一项的前缀. 析:首先用大数把Fib数列的前100000-1项算出来,注意,一定不能是100000,要不然会WA的,然后每个数取前40位,不足4 ...

  6. hdu 4099 Revenge of Fibonacci 大数+压位+trie

    最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...

  7. UVA 11488 Hyper Prefix Sets (字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  9. 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 ...

随机推荐

  1. 【爬虫】 爬虫请求json数据,返回乱码问题的解决

    from django.http import JsonResponse from rest_framework.utils import json from utils import request ...

  2. ES6 - 基础学习(8): Promise 对象

    概述 Promise是异步编程的一种解决方案,比传统的解决方案(多层嵌套回调.回调函数和事件)更强大也更合理.从语法上说,Promise是一个对象,从它可以获取异步操作的消息,Promise 还提供了 ...

  3. 【转载】ibit-mybatis介绍

    原文链接:ibit-mybatis介绍 概述    ibit-mybatis是一个Mybatis的增强工具,在Mybatis的基础上增加了新的特性与功能,志在简化开发流程.提高开发效率. 特性 无侵入 ...

  4. Orleans[NET Core 3.1] 学习笔记(四)( 2 )获取Grain的方式

    简介 在这一节,我们将介绍如何在Silo和Client中获取Grain及调用Grain Grain获取方式 从Grain内部获取: //根据特定的Key值创建或获取指定的Grain IStudent ...

  5. MySQL日志文件和InnoDB引擎文件简介

    MySQL和InnoDB的关系不在这里介绍了.但是大家都知道其中相关的文件很多,类型很多.看文件名就有点分布清楚了.所以在这里简单介绍下他们的文件. 我们直接看文件列表以及在后面直接加注释.做笔记. ...

  6. ftp下载目录文件 不需要ftp脚本

    ftp下载目录文件 不需要ftp脚本 wget ftp://192.168.1.37:21/checkpoints --ftp-user=ftpadmin --ftp-password=gaofeng ...

  7. vue 3.0 项目搭建移动端 (六) 命名路由同级控制

    const Tabbar = () => import('@/components/Tabbar'); export default [ { path: '/', name: 'home', c ...

  8. 爬取漫画DB上的《浪客行》

    漫画链接:https://www.manhuadb.com/manhua/324 建议:早上爬,速度较快. 天下无双宫本武藏 代码 # https://www.manhuadb.com/manhua/ ...

  9. shiro权限认证Realm的四大用法

    一.SimpleAccountRealm public class AuthenticationTest {          SimpleAccountRealm sar=new SimpleAcc ...

  10. NIM游戏的Python实现

    可执行程序下载: 链接:https://pan.baidu.com/s/1xQedrWRBsqQRZvOe91Rvng 提取码:goi9 Nim游戏是博弈论中最经典的模型(之一),它又有着十分简单的规 ...