HDU4099 Revenge of Fibonacci(高精度+Trie)
来源:互联网 发布:应用程序的端口号干什 编辑:程序博客网 时间:2024/06/05 17:55
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
Problem Description
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.
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.
Input
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, 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.
Output
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.
Sample Input
151121231234123459989879876987658932510751761671761761763477467395610
Sample Output
Case #1: 0Case #2: 25Case #3: 226Case #4: 1628Case #5: 49516Case #6: 15Case #7: 15Case #8: 15Case #9: 43764Case #10: 49750Case #11: 10Case #12: 51Case #13: -1Case #14: 1233Case #15: 22374
Source
2011 Asia Shanghai Regional Contest
【思路】
高精度加法+Trie。
离线处理出F(99999)以内所有的F在长度40以内的前缀并构造一棵字典树。因为精度原因,保留60位进行加法计算就可以达到精确。
Trie的结点维护一个val表示经过该节点的所有字符串中的最小标号,对应每一个查询用O(maxn)的时间求解。
总的时间复杂度为O(99999*60+T*maxn)。
【代码】
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++) 6 using namespace std; 7 8 const int sigmasize = 10; 9 //Trie相关 10 struct Node{ 11 int val; 12 Node* next[sigmasize]; 13 }; 14 struct Trie{ 15 Node *root; 16 Trie() { 17 root=new Node; 18 for(int i=0;i<sigmasize;i++) root->next[i]=NULL; 19 root->val=0; 20 } 21 int idx(char c) { return c-'0'; } 22 void insert(char* s,int v) { 23 int n=strlen(s); 24 Node* u=root; 25 for(int i=0;i<min(n,41);i++) { 26 int c=idx(s[i]); 27 if(u->next[c]==NULL) { 28 Node* tmp=new Node; 29 tmp->val=0; 30 for(int i=0;i<sigmasize;i++) tmp->next[i]=NULL; 31 u->next[c]=tmp; 32 } 33 u=u->next[c]; 34 if(u->val==0) u->val=v; 35 } 36 if(u->val==0) u->val=v; //存储最小的F 37 } 38 int find(char* s) { 39 int n=strlen(s); 40 Node* u=root; 41 for(int i=0;i<min(n,41);i++) { 42 int c=idx(s[i]); 43 if(u->next[c]==NULL) return 0; 44 else u=u->next[c]; 45 } 46 return u->val; 47 } 48 void del(Node *root) { 49 for(int i=0;i<sigmasize;i++) { 50 if(root->next[i]!=NULL) del(root->next[i]); 51 } 52 free(root); 53 } 54 }trie; 55 //题目相关 56 int n; 57 const int maxn = 100; 58 char F1[maxn],F2[maxn],Ftmp[maxn],s[maxn]; 59 char d[maxn]; 60 61 void Add(char *a,char *b,char *c) 62 { 63 int i,j,k,aa,bb,t=0,p=0; 64 aa=strlen(a)-1,bb=strlen(b)-1; 65 while(aa>=0||bb>=0) { 66 if(aa<0)i=0; else i=a[aa]-'0'; 67 if(bb<0)j=0; else j=b[bb]-'0'; 68 k=i+j+t; 69 d[p++]=k%10+'0'; 70 t=k/10; 71 aa--,bb--; 72 } 73 while(t) { 74 d[p++]=t%10+'0'; 75 t=t/10; 76 } 77 for(i=0;i<p;i++) c[i]=d[p-i-1]; 78 c[p]='\0'; 79 } 80 81 int main() { 82 F1[0]='1',F1[1]='\0'; 83 F2[0]='1',F2[1]='\0'; 84 trie.insert(F1,1),trie.insert(F2,1); 85 FOR(i,2,100000-1) { 86 strcpy(Ftmp,F2); 87 int len1=strlen(F1),len2=strlen(F2); 88 if(len1>60) { 89 F1[60]=F2[60]='\0'; 90 } 91 Add(F1,F2,F2); 92 trie.insert(F2,i+1); 93 strcpy(F1,Ftmp); 94 } 95 int T,kase=0; 96 scanf("%d",&T); 97 while(T--) { 98 scanf("%s",s); 99 int ans=trie.find(s);100 if(!ans) ans=-1; else ans--;101 printf("Case #%d: %d\n",++kase,ans);102 }103 trie.del(trie.root);104 return 0;105 }
0 0
- HDU4099 Revenge of Fibonacci(高精度+Trie)
- HDU4099-Revenge of Fibonacci(trie树+数学基础)
- HDU4099 Revenge of Fibonacci
- HDU 4099 Revenge of Fibonacci(高精度加法+字典树Trie)
- hdu 4099 Revenge of Fibonacci (Trie树)
- UVa 12333 - Revenge of Fibonacci(高精度计算+字典树)
- UVa 12333 - Revenge of Fibonacci(字典树+高精度压位)
- HDU 4099 Revenge of Fibonacci(高精度+字典树)
- HDU-#5018 Revenge of Fibonacci(模拟)
- HDU 5018 Revenge of Fibonacci(数学)
- hdu 5018 Revenge of Fibonacci(水题)
- 12333 - Revenge of Fibonacci
- UVa12333 - Revenge of Fibonacci
- UVA12333--Revenge of Fibonacci
- HDU 4099 Revenge of Fibonacci
- hdu 4099 Revenge of Fibonacci
- UVa 12333 - Revenge of Fibonacci
- HDU 4099 Revenge of Fibonacci
- 设计Qt风格的C++API
- bzoj2124 等差子序列(hash+线段树)
- 递归求最大公约数
- HDU1251 统计难题(Trie)
- HDU1247 Hat’s Words(Trie的动态内存版本)
- HDU4099 Revenge of Fibonacci(高精度+Trie)
- HDU2222 Keywords Search(AC自动机)
- 1019. 数字黑洞
- Android知识库
- poj1743 Musical Theme(后缀数组|后缀自动机)
- 正则表达式中的匹配,替换,切割,获取,这四个使用时具体用哪一个,进行案例分析
- 数据结构 - 简单的单链表结构
- poj3261 Milk Patterns(后缀数组)
- SQL 查询地理信息中指定距离范围内的记录