描述

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.

输入

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.

输出

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.

样例输入

15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610

样例输出

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

题意

求最小第几个斐波那契数前缀等于这个数

题解

看到这种查询多又是前缀的很容易想到预处理+trie

预处理硬算再取前40位很明显会TLE,位数太多了,只取40位会发现精度不够,为了精确取到了前60位,和暴力打表对上

查询前缀,直接插入到trie然后查询就行了

代码

 #include<bits/stdc++.h>
using namespace std; //trie
const int maxn=5e6+;
int cnt,ch[maxn][],val[maxn];
int getIdx(char a){return a-'';}
void insert(char st[],int d){
int u=,l=strlen(st);
for(int i=;i<l&&i<;i++){
int k=getIdx(st[i]);
if(!ch[u][k]){
val[cnt]=d;
ch[u][k]=cnt++;
memset(ch[cnt],,sizeof ch[cnt]);
}
u=ch[u][k];
}
}
int query(char st[]){
int u=,l=strlen(st);
for(int i=;i<l;i++){
int k=getIdx(st[i]);
if(!ch[u][k])return -;
u=ch[u][k];
}
return val[u];
} char c[],str[];
void add(char a[],char b[],char back[])
{
int x,y,z,i=strlen(a)-,j=strlen(b)-,k=,p=;
while(i>=||j>=)
{
if(i<)x=;
else x=a[i]-'';
if(j<)y=;
else y=b[j]-'';
z=x+y+p;
c[k++]=z%+'';
p=z/;
i--,j--;
}
if(p>)c[k++]=p+'';
for(i=;i<k;i++)back[i]=c[k--i];
back[k]='\0';
}
void init()
{
cnt=;
memset(ch[],,sizeof ch[]);
memset(val,0x3f3f3f3f,sizeof val);
char a[],b[],ans[];
a[]='',a[]=;
b[]='',b[]=;
insert(a,);
for(int i=;i<;i++)
{
if(strlen(b)>)a[strlen(a)-]=,b[strlen(b)-]=;
add(a,b,ans);
insert(ans,i);
strcpy(a,b);
strcpy(b,ans);
}
}
int main(){
init();
int t,T=;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
printf("Case #%d: %d\n",T++,query(str));
}
return ;
}

TZOJ 3820 Revenge of Fibonacci(大数+trie)的更多相关文章

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

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

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

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

  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. HDU4099-Revenge of Fibonacci(trie树+数学基础)

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

  6. HDU 4099 大数+Trie

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

  7. UVA 12333 Revenge of Fibonacci

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

  8. [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci

    题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...

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

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

随机推荐

  1. hsdfz -- 6.16 -- day1

    恩这回不写游记了 按照老师要求记录今天的心里路程:这题似乎可做期望得分150->日部分分似乎不是很显然->a题似乎是结论题,大力猜一波结论->过不了样例,先看b题->b题动态树 ...

  2. [Java] 方法 -- 繼承關係

    public class test { void show() { System.out.println("父類別"); } } public class test2 extend ...

  3. 图像小波变换去噪——MATLAB实现

    clear; [A,map]=imread('C:\Users\wangd\Documents\MATLAB\1.jpg'); X=rgb2gray(A); %画出原始图像 subplot(,,);i ...

  4. mysql ibdata1

    ibdata1是什么? Mysql ibdata1即Innodb data1缩写,是innodb引擎的表空间,用于存放 数据字典Data dictionary:  只读的表,存储对象的相关信息,如占用 ...

  5. python成功之道

    https://blog.ansheng.me/article/python-full-stack-way

  6. springJdbc(jdbcTemplate)事物拦截失效问题解决

    先贴上web.xml和spring-jdbc.xml代码: web.xml代码: <context-param> <param-name>contextConfigLocati ...

  7. Django中media的配置

    Django中media的配置 Django中media文件夹是我们文件(比如头像.文件.视频等)数据十分重要的存放处,这里以用户头像的上传以及media文件的访问为例为大家详细讲解下media的相关 ...

  8. [UE4]Window Title Bar Area

    一.Window Title Bar Area.windows窗口拖拽控件 二.window Buttons Enabled,在控件的右上角显示:最小化.最大化,关闭按钮; Toggle Fullsc ...

  9. DllImport使用

    1.Dll引用路径 (1)exe运行程序所在的目录 (2)System32目录 (3)环境变量目录 (4)自定义路径,如:DllImport(@"C:\OJ\Bin\Judge.dll&qu ...

  10. spring cloud 学习目录

    1.spring cloud简单示例 2.spring cloud快速入门 3.spring cloud 常用 4.spring cloud 原理 5.spring cloud 源码分析 6.spri ...