TZOJ 3820 Revenge of Fibonacci(大数+trie)
描述
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)的更多相关文章
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- HDU4099 Revenge of Fibonacci(高精度+Trie)
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...
- HDU4099-Revenge of Fibonacci(trie树+数学基础)
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- HDU 4099 大数+Trie
Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/ ...
- UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [刷题]算法竞赛入门经典(第2版) 5-15/UVa12333 - Revenge of Fibonacci
题意:在前100000个Fibonacci(以下简称F)数字里,能否在这100000个F里找出以某些数字作为开头的F.要求找出下标最小的.没找到输出-1. 代码:(Accepted,0.250s) / ...
- hdu 4099 Revenge of Fibonacci 字典树+大数
将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...
随机推荐
- activiti学习第一天
公司项目组在考虑工作流,首选了activiti,首先我们要明确为什么要使用activiti,有什么好处. 在工作中有些项目会用到工作流,如果简单的项目,我们就无需使用类似activiti.jbpm等工 ...
- Javascript var 和 let 的区别
Javascript var 和 let 的区别 var 是函数块的全局变量. let 是代码块的局部变量. let 变量不会提升,如果先使用后定义会 undefind. 参考: https://de ...
- hadoop完全分步式搭建
实验环境介绍 4台机器,规划如下: 计算机名 IP地址 角色 master 192.168.138.200 NameNode,SecondaryNameNode,ResourceManager sla ...
- 阅读 video on-screen display v6.0笔记
阅读 video on-screen display v6.0笔记 关于axi总线时钟的区分 需要弄清楚的是aclk, aclken, aresetn 信号是和video 有关的,axi4-lite的 ...
- mongodb与mysql命令详细对比
传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(collection).文档对象(docu ...
- git tag 常用操作
1.获取最新tag(获取不到就多获取几次) git fetch origin 或者 git fetch origin <tagname> 2. checkout tag到本地分支(如果看 ...
- 如何系统学习知识图谱-15年+IT老兵的经验分享
一.前言 就IT而言,胖子哥算是老兵,可以去猝死的年纪,按照IT江湖猿龄的规矩,也算是到了耳顺之年:而就人工智能而言,胖子哥还是新人,很老的新人,深度学习.语音识别.人脸识别,知识图谱,逐个的学习了一 ...
- oracle数据库报错ora-01653表空间扩展失败解决方案
1)ora-01653错误截图: 可以看到有两张表的insert受到了影响,都是在USERS表空间里.用以下SQL查看表空间使用情况: "表空间大小(M)",(a.bytes &q ...
- jdk8 Metaspace 调优
简介 jdk8的元空间的初始大小是21M,如果启动后GC过于频繁,请将该值设置得大一些. 更多Meatspace内容见<Metaspace 之一:Metaspace整体介绍(永久代被替换原因.元 ...
- Python的xml模块
先来一段xml代码 <?xml version="1.0"?> <data> <country name="Liechtenstein&qu ...