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 ...
随机推荐
- 用原生JS&PHP简单的AJAX实例
功能介绍: 1)file.html 使用 xmlhttp 请求服务器端文件 text ,并更新 file.html 的部分内容 2)update.html 使用 xmlhttp 通过 filewrit ...
- Ajax0002: 省市县三级联动案例
- 如何在 vue 中添加权限控制管理?---vue中文社区
前言 在一个项目中,一些功能会涉及到重要的数据管理,为了确保数据的安全,我们会在项目中加入权限来限制每个用户的操作.作为前端,我们要做的是配合后端给到的权限数据,做页面上的各种各样的限制. 需求 因为 ...
- 题解 Luogu P3370
讲讲这题的几种做法: 暴力匹配法 rt,暴力匹配,即把字符串存起来一位一位判相等 时间复杂度$ O(n^2·m) $ 再看看数据范围 \(n\le10^5,m\le10^3\) 当场爆炸.当然有暴力分 ...
- 处理方法返回ModelAndView类型
1.请求 <a href="test">测试</a> 2.处理方法 @RequestMapping("/test") public Mo ...
- JVM第二篇 类加载子系统
1.内存结构概述 简图 详细 2.类加载器与类加载的过程 类加载器子系统负责从文件系统或者网络中加载Class文件,class文件在文件开头有特定的文件标识[CA FE BA BY ...
- 开启WIndows10 未经身份验证的来宾访问策略以及SMB1
打开记事本编辑保存至.vbs 以管理员身份运行 Set obj = createobject("wscript.shell") obj.run ("reg add HKL ...
- 剑指offer-面试题55-平衡二叉树-递归
/* 题目: 判断二叉树是否为平衡二叉树. */ /* 思路: 判断二叉树的左子树和右子树高度相差是否为1. */ #include<iostream> #include<cstri ...
- opencv —— 同时识别三种颜色
要点: 1.识别一种颜色 minH = ; //色相 maxH = ; minS = ; //饱和度 maxS = ; minV = ; // inRange(原图像, 最小值的范围, 最大值的范围, ...
- Chapter2二分与前缀和
Chapter 2 二分与前缀和 +++ 二分 套路 如果更新方式写的是R = mid, 则不用做任何处理,如果更新方式写的是L = mid,则需要在计算mid是加上1. 1.数的范围 789 #in ...