描述

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. tgp助手开启逆战游戏无反应

    tgp助手开启逆战游戏无反应(一直显示正在运行游戏)就是没有游戏的登录界面 解决的一些方法(不一定有效): 检查显卡的驱动 检查游戏文件是否损坏 检查是否开启的防护软件程序

  2. PythonStudy——数据类型转化 Data type conversion

    类型转换 1.数字类型:int() | bool() | float() 2.str与int:int('10') | int('-10') | int('0') | float('-.5') | fl ...

  3. 【DB2】SQL1585N 由于没有具有兼容页面大小的可用系统临时表空间,因此无法创建临时表。SQLSTATE=54048

    自己写了一段SQL,SQL中包含ORDER BY 字句,但是在执行的时候报错如下: 经过查询发现是由于临时表空间的PAGESIZE不够大,可考虑建16k或者32k PAGESIZE的表空间 示例如下: ...

  4. dubbo 基础入门

    一.什么是dubbo? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,说白了就是个远程服务调用的分布式框架. dubbo产生的背景 ① 单一 ...

  5. nginx实现按日期进行日志分割

    1:nginx的访问日志按日期分割,也就是每天的零点把前一天的访问日志以日期的形式备份,然后重新打开一份访问日志,这里的kill -USR1 $pid 重新打开访问日志,必须得把原来的mv,如果存在的 ...

  6. ajax请求完成执行的操作

    var createAjax = $("#createId").ajax(function(){ //ajax操作 }); $.when(createAjax).done(func ...

  7. requests库(爬虫)

    北京理工大学嵩天老师的课程:http://www.icourse163.org/course/BIT-1001870001 官方文档:http://docs.python-requests.org/e ...

  8. IIS6.0+win2003部署MVC网站的一些问题

    安装iis,framework环境不谈.MVC网站部署 步骤: 1.为程序新建一个应用程序池(将default的那个程序池作为模板就可以了) 2.web服务扩展一些启用一些必要的服务 3.新建网站 描 ...

  9. Memcached 使用备注

    ICSharpCode.SharpZipLib未能加载文件或程序集 Memcached使用的是0.84版本的dll,vs2013带的是0.86版本 在web.config中<runtime> ...

  10. (整理)EF分页的实现

    最近做一个小功能,需要数据分页,因为小框架使用的是EF,因此查询了一下EF的分页. EF分页主要用到了skip和take两个方法: GetListBy(lamda xxxxx).skip(PageSi ...