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. 13.Android-ListView使用、BaseAdapter/ArrayAdapter/SimpleAdapter适配器使用

    1.ListView ListView 是 Android 系统为我们提供的一种列表显示的一种控件,使用它可以用来显示我们常见的列表形式.继承自抽象类 AdapterView.继承图如下所示: 以微信 ...

  2. redis 5.0.7 源码阅读——字典dict

    redis中字典相关的文件为:dict.h与dict.c 与其说是一个字典,道不如说是一个哈希表. 一.数据结构 dictEntry typedef struct dictEntry { void * ...

  3. 【公告】Hello World!

    Hi! 这里 是 华中师大一附中 2019 级信息组 官方博客 ! ^_^ 我们将在这里分享一些难题的做法,帮助大家共同学习. 也欢迎同是OIer的你加入我们哦!

  4. RestTemplate + okhttp 实现远程调用

    1. 添加依赖 <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --> <dependency ...

  5. 聊聊spring之贯穿全局的重要对象BeanDefinition

    BeanDefinition 在 spring 中贯穿始终,spring 要根据 BeanDefinition 对象来实 例化 bean,只要把解析的标签,扫描的注解类封装成 BeanDefiniti ...

  6. 如何在 vue 中添加权限控制管理?---vue中文社区

    前言 在一个项目中,一些功能会涉及到重要的数据管理,为了确保数据的安全,我们会在项目中加入权限来限制每个用户的操作.作为前端,我们要做的是配合后端给到的权限数据,做页面上的各种各样的限制. 需求 因为 ...

  7. 剑指offer-面试题58_2-左旋转字符串-字符串

    /* 题目: 将字符串的前sep个字符转移到字符串尾部. */ /* 思路: 更好的方法: 先翻转前sep个字符,再翻转后面的字符,最后全体翻转. */ #include<iostream> ...

  8. 蓝眼睛与红眼睛(The blue-eyed islanders puzzle)

    澳大利亚的华裔数学神童陶哲轩曾在网上贴出来一个问题 The blue-eyed islanders puzzle 让大家思考,逗大家玩儿. 说一个岛上有100个人,其中有5个红眼睛,95个蓝眼睛.这个 ...

  9. javaSE学习笔记(16)---网络编程

    javaSE学习笔记(16)---网络编程 基本概念 如今,计算机已经成为人们学习.工作.生活必不可少的工具.我们利用计算机可以和亲朋好友网上聊天,也可以玩网游.发邮件等等,这些功能实现都离不开计算机 ...

  10. Python爬虫连载9-JS加密之“盐”​、ajax请求

    一.JS加密之“盐”​ 1.salt属性“盐":多用于密码学,比如我们的银行卡是六位密码,但是实际上在银行的系统里,我们输入密码后,会给原始的密码添加若干字符,形成更加难以破解的密码.这个过 ...