https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3755
Revenge of Fibonacci
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 914 Accepted Submission(s): 197
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
15
1
12
123
1234
12345
9
98
987
9876
98765
89
32
51075176167176176176
347746739
5610
Sample Output
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
Source
这题就是求以某串数字开头的斐波那契数列。
因为只要前40位数字。所以在加的时候长度大于50左右就截掉个位上的,保留高位。即从最高位开始插入进字典树,注意超过100000输出-1,也包括100000本事,即只需处理到9999项即可。
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define INF 0x3f3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int t,cast,f1[100],f2[100],f3[100]; char a[55]; struct trie { ll isend; struct trie *next[11]; trie() { isend=-1; for(int i=0;i<=9;i++) next[i]=NULL; } }; trie *root=new trie(); void insert(trie *root,char *s,int k) { trie *p=root; trie *tmp; int len=strlen(s); for(int i=0;i<len;i++) { if(p->next[s[i]-'0']==NULL) { tmp=new trie(); p->next[s[i]-'0']=tmp; } p=p->next[s[i]-'0']; if(p->isend<0) p->isend=k; } } int find(trie *root,char *s) { trie *p=root; int k; int len=strlen(s); for(int i=0;i<len;i++) { p=p->next[s[i]-'0']; if(p==NULL) return -1; else k=p->isend; } return k; } void init() { char b[100]="1"; memset(f1,0,sizeof(f1)); memset(f2,0,sizeof(f2)); memset(f3,0,sizeof(f3)); f1[0]=1;f2[0]=1; insert(root,b,0); for(int i=2;i<100000;i++) { memset(b,0,sizeof(b)); int cnt=0,k; for(int j=0;j<65;j++) { f3[j]=f1[j]+f2[j]+cnt; cnt=f3[j]/10; f3[j]%=10; } for(int j=64;j>=0;j--) { if(f3[j]){k=j;break;} } int pos=0; for(int j=k;j>=0;j--) { b[pos++]=f3[j]+'0'; if(pos>=40) break; } insert(root,b,i); if(k>60) { for(int j=1;j<64;j++)//舍弃个位我i,保留高位 f3[j-1]=f3[j]; for(int j=1;j<64;j++) f2[j-1]=f2[j]; } for(int j=0;j<65;j++) f1[j]=f2[j]; for(int j=0;j<65;j++) f2[j]=f3[j]; } } int main() { init(); scanf("%d",&t); cast=t; while(t--) { scanf("%s",a); printf("Case #%d: ",cast-t); printf("%d ",find(root,a)); } return 0; }