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. 手动使用I2C协议写入24C02C

    刚尝试用AT89C52单片机使用IIC总线协议读写AT24C02C,我忽然想能否用手动调整开关的方式写入AT24C02C?于是,便尝试了一下,结果果然成功了. 关于IIC总线,这篇文章写的很详细:ht ...

  2. toj 3019 Hidden Password (最小表示法)

    Hidden Password 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交: 53 测试通过: 19 描述 Some time the progr ...

  3. jenkin docker node 自动化部署配置

    jenkins 安装必须插件 NodeJS Plugin Publish Over SSH 1:新建一个任务,选择构建一个自由风格的软件项目 2:源码管理选择Git 2.1 填写Git项目地址Repo ...

  4. You (oracle) are not allowed to use this program (crontab)

      检查一台ORACLE数据库服务器的crontab作业(用户为oracle,实际环境中可能为oracle.也有可能是其它用户)时,发现出现下面提示信息: $ crontab -l You (orac ...

  5. echarts配置环形饼图的参数,以及牵引线显示百分比,中间数据

    最近项目有多处是用echarts的,有环形图,折线图,饼图,总结了一下. 本次主要讲环形图,折线图在下期. 这个是最终的效果图.下面附上代码 //三种占比 var myChartType = echa ...

  6. 《自拍教程21》mediainfo_多媒体文件查看工具

    mediainfo命令介绍 mediainfo.exe(Linux/iMac下是未带后缀的mediainfo), 是一款音视频图片文件的信息查询工具, 常用于查看多媒体文件的视频流信息,音频流信息,字 ...

  7. 解决 Windows 编译 Fast R-CNN 的 bbox 和 nms 出现的错误 error: Unable to find vcvarsall.bat

    在 Windows 下安装一个底层的 Python 包时(Fast R-CNN 的 bbox 和 nms),遇到 error: Unable to find vcvarsall.bat 错误,看到这个 ...

  8. MySQL和MariaDB安全初始化

    通过yum安装mysql(5.x)后往往需要进行一些安全类的初始化设置: 安装完数据库后执行mysql_secure_installation命令,会出现安全相关的交互界面. 按提示操作.

  9. Umi 小白纪实(二)—— model 的注册与使用

    Umi 通常会搭配 Dva 使用,用于管理页面状态和逻辑 一.注册 model 首先需要在 .umirc.js 中启用 dva 插件 export default { plugins: [ ['umi ...

  10. Linux下的python3,virtualenv,Mysql、nginx、redis等常用服务安装配置

    Linux下的python3,virtualenv,Mysql.nginx.redis等常用服务安装配置   学了前面的Linux基础,想必童鞋们是不是更感兴趣了?接下来就学习常用服务部署吧! 安装环 ...