HDU4099 Revenge of Fibonacci(高精度+Trie)
Revenge of Fibonacci
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 2582 Accepted Submission(s): 647
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.
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.
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.
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610
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。
离线处理出F()以内所有的F在长度40以内的前缀并构造一棵字典树。因为精度原因,保留60位进行加法计算就可以达到精确。
Trie的结点维护一个val表示经过该节点的所有字符串中的最小标号,对应每一个查询用O(maxn)的时间求解。
总的时间复杂度为O(99999*60+T*maxn)。
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int sigmasize = ;
//Trie相关
struct Node{
int val;
Node* next[sigmasize];
};
struct Trie{
Node *root;
Trie() {
root=new Node;
for(int i=;i<sigmasize;i++) root->next[i]=NULL;
root->val=;
}
int idx(char c) { return c-''; }
void insert(char* s,int v) {
int n=strlen(s);
Node* u=root;
for(int i=;i<min(n,);i++) {
int c=idx(s[i]);
if(u->next[c]==NULL) {
Node* tmp=new Node;
tmp->val=;
for(int i=;i<sigmasize;i++) tmp->next[i]=NULL;
u->next[c]=tmp;
}
u=u->next[c];
if(u->val==) u->val=v;
}
if(u->val==) u->val=v; //存储最小的F
}
int find(char* s) {
int n=strlen(s);
Node* u=root;
for(int i=;i<min(n,);i++) {
int c=idx(s[i]);
if(u->next[c]==NULL) return ;
else u=u->next[c];
}
return u->val;
}
void del(Node *root) {
for(int i=;i<sigmasize;i++) {
if(root->next[i]!=NULL) del(root->next[i]);
}
free(root);
}
}trie;
//题目相关
int n;
const int maxn = ;
char F1[maxn],F2[maxn],Ftmp[maxn],s[maxn];
char d[maxn]; void Add(char *a,char *b,char *c)
{
int i,j,k,aa,bb,t=,p=;
aa=strlen(a)-,bb=strlen(b)-;
while(aa>=||bb>=) {
if(aa<)i=; else i=a[aa]-'';
if(bb<)j=; else j=b[bb]-'';
k=i+j+t;
d[p++]=k%+'';
t=k/;
aa--,bb--;
}
while(t) {
d[p++]=t%+'';
t=t/;
}
for(i=;i<p;i++) c[i]=d[p-i-];
c[p]='\0';
} int main() {
F1[]='',F1[]='\0';
F2[]='',F2[]='\0';
trie.insert(F1,),trie.insert(F2,);
FOR(i,,-) {
strcpy(Ftmp,F2);
int len1=strlen(F1),len2=strlen(F2);
if(len1>) {
F1[]=F2[]='\0';
}
Add(F1,F2,F2);
trie.insert(F2,i+);
strcpy(F1,Ftmp);
}
int T,kase=;
scanf("%d",&T);
while(T--) {
scanf("%s",s);
int ans=trie.find(s);
if(!ans) ans=-; else ans--;
printf("Case #%d: %d\n",++kase,ans);
}
trie.del(trie.root);
return ;
}
HDU4099 Revenge of Fibonacci(高精度+Trie)的更多相关文章
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
题意:对给定前缀(长度不超过40),找到一个最小的n,使得Fibonacci(n)前缀与给定前缀相同,如果在[0,99999]内找不到解,输出-1. 思路:用高精度加法计算斐波那契数列,因为给定前缀长 ...
- TZOJ 3820 Revenge of Fibonacci(大数+trie)
描述 The well-known Fibonacci sequence is defined as following: Here we regard n as the index of the F ...
- hdu4099 Revenge of Fibonacci
题意:给定fibonacci数列,输入前缀,求出下标.题目中fibonacci数量达到100000,而题目输入的前缀顶多为40位数字,这说明我们只需要精确计算fibinacci数前40位即可.查询时使 ...
- UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树
题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...
- hdu4099 Revenge of Fibonacci 字典树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 思想很容易想到 就是预处理出前10w个的fib数,然后建树查询 建树时只用前40位即可,所以在计 ...
- HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci Problem Description The well-known Fibonacci sequence is defined as following: ...
- 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项范围内,输 ...
- UVa12333 Revenge of Fibonacci
高精度 trie 暴力预处理出前100000个fibonacci数,将每个数的前40位数字串插入到trie中,记录每个结点最早可以由哪个数字串到达. 然后依次回答询问即可. 存fibonacci数的数 ...
随机推荐
- .net 安装Swagger
官网:http://swagger.io/ 教程:http://www.wmpratt.com/swagger-and-asp-net-web-api-part-1/ 1:安装Dll: https:/ ...
- Chrome浏览器允许跨域请求配置
最近有个做数据标注的任务,但是标注平台是别人公司的,他们又不愿意对平台进行升级改造: 其实要改的地方也很简单,就是对页面做一些处理,做一些脚本控制. 没办法,做了个 iframe 给她嵌入到我们自己的 ...
- ^(bitwise exclusive Or).
一个数,进行异或同一个数两次,将得到原来的数,例如: 6 ^ 4 ^ 4 = 6; 0000-0000-0000-0110 ^ 0000-0000-0000-0100 ---------------- ...
- 【读书笔记】管道和FIFO
管道 提供一个单路(单向)数据流,可以为两个不同进程提供进程间的通信手段 #include <unistd.h> ]); 返回两个文件描述符,fd[0](读) 和 fd[1](写) 管道间 ...
- bash: ./configure: 权限不够 怎么办?
configure没有执行权限 通过chmod给其加上x权限 chmod +x configure 再在该用户下执行 ./configure
- c# 连接oracle 读取数据
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- java开发规范总结_命名规范
规范需要平时编码过程中注意,是一个慢慢养成的好习惯 1.文件 1.属性文件后缀为properties,并且符合java中i18n的规范: 2.对于各产品模块自己的配置文件必须放置在自己模块的con ...
- Jmeter软件测试1--webservice测试
写在前言 程序猿一枚,原本就是负责安安静静的撸代码,后来公司让兼任下测试的工作,还得照顾下面的几个测试兄弟,无奈本人毫无软件测试理论知识,下面的测试兄弟也是初级水平,又面临公司要求做webservic ...
- 《bunzip2命令》-linux命令五分钟系列之八
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- ubuntu 14.04.02 LTS 启动项误写入 /dev/sda1 (win 7 loader) 修复
问题描述: 在win7下安装Ubuntu14.04,由于启动项 /boot loader 安装位置错误(/dev/sda1 (win 7 loader) )导致无法进入Windows(在GRUB界面能 ...