Revenge of Fibonacci

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/Others)

Total Submission(s): 1944    Accepted Submission(s): 446

Problem Description
The well-known Fibonacci sequence is defined as following:





  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
 
Source

题目大意:
T组測试例子,问你一个数字串是哪个斐波那契数列的前缀。要求下标要最小

做法:
先算斐波那契数。由于数字较大,所以要用大数模板。考虑到询问的数字串最多为40个,所以在插入trie树时能够选择插入<=40个,这样能够节省非常大的内存。

大数模板网上找的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 7000000;
const int INF = 1e8;
int ch[maxn][10];
int val[maxn];
int cnt;
char c[200];
char str[200];
void add(char a[],char b[],char back[]){ int i=strlen(a)-1,j=strlen(b)-1,k=0;
int x,y,z;
int up=0;
while(i>=0||j>=0)
{
if(i<0)x=0;
else x=a[i]-'0';
if(j<0)y=0;
else y=b[j]-'0';
z=x+y+up;
c[k++]=z%10+'0';
up=z/10;
i--;
j--;
}
if(up>0)c[k++]=up+'0';
for(i=0;i<k;i++)back[i]=c[k-1-i];
back[k]='\0';
}
int getIdx(char a){
return a-'0';
}
void insert(char st[],int d){
int u = 0;
for(int i = 0; i < strlen(st) && i < 42; i++){
int k = getIdx(st[i]);
if(!ch[u][k]){
val[cnt] = d;
ch[u][k] = cnt++;
memset(ch[cnt],0,sizeof ch[cnt]);
}
u = ch[u][k];
}
}
int query(char st[]){
int u = 0;
for(int i = 0; i < strlen(st); i++){
int k = getIdx(st[i]);
if(!ch[u][k]){
return -1;
}
u = ch[u][k];
}
return val[u];
}
void init(){
cnt = 1;
memset(ch[0],0,sizeof ch[0]);
for(int i = 0; i < maxn; i++)
val[i] = INF;
char a[200],b[200],ans[200];
a[0] = '1',a[1] = 0;
b[0] = '1',b[1] = 0;
insert(a,0);
for(int i = 2; i < 100000; i++){
if(strlen(b) > 70){
a[strlen(a)-1] = 0;
b[strlen(b)-1] = 0;
}
add(a,b,ans);
insert(ans,i);
strcpy(a,b);
strcpy(b,ans);
}
}
int main(){
init();
int ncase,T=1;
cin >> ncase;
while(ncase--){
cin >> str;
printf("Case #%d: %d\n",T++,query(str));
}
return 0;
}



版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU4099-Revenge of Fibonacci(trie树+数学基础)的更多相关文章

  1. hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

  2. hdu4099 Revenge of Fibonacci 字典树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 思想很容易想到 就是预处理出前10w个的fib数,然后建树查询 建树时只用前40位即可,所以在计 ...

  3. HDU4099 Revenge of Fibonacci(高精度+Trie)

    Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/ ...

  4. HDU 4099 Revenge of Fibonacci Trie+高精度

    Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...

  5. hdu 4099 Revenge of Fibonacci 字典树+大数

    将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...

  6. hdu4099 Revenge of Fibonacci

    题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使 ...

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

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

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

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

  9. UVA 12333 Revenge of Fibonacci

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

随机推荐

  1. Setup iOS Development Environment.

    Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...

  2. CompletionService 和ExecutorService的区别和用法

    JavaSE5的Java.util.concurrent包中的执行器(Executor)将为你管理Thread对象,从而简化了并发编程.Executor在客户端和执行任务之间提供了一个间接层,Exec ...

  3. Sub-process /usr/bin/dpkg returned an error code (1)错误解决办法

    之前通过wine安装了一款软件,不经常用,也没有拆卸 可是之后呢,每次更新都要提示更新那个软件,更新的时候又总是找不到更新源 无奈之下,我通过软件中心移除wine,没想到竟然碰到依赖问题,没有拆卸成功 ...

  4. MS SQL Server的STRING_SPLIT和STRING_AGG函数

    在较新版本的SQL中,出现有2个函数,STRING_SPLIT和STRING_AGG,前者是把带有分隔的字符串转换为表,而后者却是把表某一表转换为以某种字符分隔的字符串. 如下面: DECLARE @ ...

  5. 前端开发必备调试工具(Chrome的F12自带的功能和firebug插件差不多)

    前端开发必备调试工具(Chrome的F12自带的功能和firebug插件差不多) 一.总结 Chrome的F12自带的功能和firebug插件差不多 二.前端开发必备调试工具 在前端开发中我们经常会要 ...

  6. Opencv Sift算子特征提取与匹配

    SIFT算法的过程实质是在不同尺度空间上查找特征点(关键点),用128维方向向量的方式对特征点进行描述,最后通过对比描述向量实现目标匹配. 概括起来主要有三大步骤: 1.提取关键点: 2.对关键点附加 ...

  7. Android程序解析XML文件的方法及使用PULL解析XML案例

    一.一般解析XML文件的方法有SAX和DOM.PULL (1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信 ...

  8. 全栈JavaScript之路( 二十五 )訪问元素的样式

    不论什么支持style 特性的元素在 ,在其DOM  节点 对象中都有一个 style 属性与之相应. 这个style 对象是 CSSStyleDeclaration类型的实例,包括着html sty ...

  9. HDU 5044 Tree(树链剖分)

    HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...

  10. web项目的WEB-INF目录

    WEB-INF是Java的WEB应用的安全目录.所谓安全就是客户端无法访问,只有服务端可以访问的目录. 如果想在页面中直接访问其中的文件,必须通过web.xml文件对要访问的文件进行相应映射才能访问. ...