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.若大数加法时 ...
随机推荐
- 通过用户名&密码验证访问远程共享文件夹 C#
通过代码先在cmd中运行net use进行验证,然后就可访问共享文件了. 验证方法如下: public string connectState(string path/*要访问的文件路径*/, str ...
- mac下pycharm快捷键
[转载]https://www.cnblogs.com/leolichao/p/9329685.html Mac键盘符号和修饰键说明 ⌘ Command ⇧ Shift ⌥ Option ⌃ Cont ...
- windows server 2008 R2之取消多余的安全配置
一:取消IE浏览器的安全配置(使IE浏览器可以正常上网) 管理员禁用即可 二.取消关机时强制输入关机备注 运行gpedit.msc,选择计算机配置->管理模板->系统->提示“关机时 ...
- expect脚本实现ssh自动登录
1:简单的实现ssh登录 #!/usr/bin/expect set ip "10.0.0.142" set user "root" set password ...
- Eclipse Build path
Build Path用于设置Java的构建路径,管理Java工程所包含的资源,使工程结构清晰合理. 包括以下几项: Source Source包括 source folder和output folde ...
- Xtrabackup的安装与使用
Xtrabackup的安装与使用 1. XtraBackup 简介 XtraBackup(PXB) 工具是 Percona 公司用 perl 语言开发的一个用于 MySQL 数据库物理热备的备份工具, ...
- jenkins构建触发器详解-不登录触发远程构建详解
利用jenkins的远程构建功能,我们可以使用任何脚本,甚至定制一个Web页来控制Job的执行,但是远程构建你如果直接使用的话,老是需要登录才能执行,如何避免登录?稍微折腾了一下,调通了. 1.首先去 ...
- 1、minimum-depth-of-binary-tree
题目描述 Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the ...
- Ubuntu 14.10 下安装Ambari 问题汇总
在编译安装Ambari时候遇到了很多问题,现在记录一下 1 got error npm ERR! phantomjs@1.9.12 install while building ambari-web ...
- 网络之TCP握手总结
目录: 31.Tcp握手的一些问题? 21.Tcp三次握手及SYN攻击: 四次握手? 为什么建立连接是三次握手,而关闭连接却是四次挥手? 13.TCP释放连接四次握手 12.TCP建立连接三次握手 1 ...