描述

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. python------面向对象进阶 Socket网络编程

    一.Socket网络编程 1.七层模型,亦称OSI(Open System Interconnection)参考模型,是参考模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系 ...

  2. python socket 函数介绍

    socket 函数原型:socket.socket([family[,type[,proto]]]) family参数取值(协议族): socket.AF_INET        -->ipv4 ...

  3. 通过 JDK 自带的 javap 命令查看 SynchronizedDemo 类的相关字节码信息

    首先切换到类的对应目录执行 javac SynchronizedDemo.java 命令生成编译后的 .class 文件 然后执行 javap -c -s -v -l SynchronizedDemo ...

  4. 淘宝客知道这几个ID,收入将会提高50%

    基础问题天天说,天天有人问.这篇文章写点基础的.特别对新手的帮助会很大哦. 1,PID,做淘宝客不知道PID,赚到钱也会被冻结. 如何手动获取PID 2,单品ID,淘宝商品的唯一识别编号,和身份证一样 ...

  5. hbase hbck命令

    hbase hbck 只做检查 hbase hbck -fixMeta 根据region目录中的.regioninfo,生成meta表` hbase hbck -fixAssignments 把met ...

  6. python3.6 内置函数

    python内置函数 # encoding: utf-8 # module builtins # from (built-in) # by generator 1.145 ""&q ...

  7. 用EXCEL做快速傅立葉轉換_FFT in Excel

    转载来自:http://yufan-fansbook.blogspot.tw/2013/09/excel-fft-fast-fourier-transform02.html [Excel]-用EXCE ...

  8. c# 抽象类 抽象函数 接口

    抽象类与抽象方法: 被abstract关键字修饰的类叫做抽象类 被abstract关键字修饰的方法叫做抽象方法 1.抽象方法必须放在抽象类中 2.抽象方法不可以实现代码,用空语句替代 3.抽象方法可以 ...

  9. [ExcelHome]15个常用的Excel函数公式,拿来即用

    微软最有价值专家(MVP)祝洪忠分享15个模式化的表格公式,大家有类似问题可以直接套用. 首先声明,我这个可称不上是什么公式大全,就是给各位新人朋友们入门学习的,高手请按返回键. 1.查找重复内容 = ...

  10. NodeJS client code websocket

    var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('co ...