HDU 4099 Revenge of Fibonacci Trie+高精度
Revenge of Fibonacci
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
题解:前100000个斐波那契数,太大所以用高精度预处理出前缀,我们只存50位就可以,出现51位,我们就删除个位,保留高位,插入trie树中
///1085422276 #include<bits/stdc++.h> using namespace std; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define pb push_back inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0';ch=getchar(); }return x*f; } //**************************************** const int N=100000+50; #define maxn 100000+5 struct Trie{ int ch[N*40][11],sum[N*40],siz; void init() {mem(ch),mem(sum),siz=1;} void insertt(int c[],int index) { int u=0,len=c[0];int cc=0; for(int i=1;i<=min(len,41);i++) { int v=c[i]; if(ch[u][v]==0) { sum[siz] = index; ch[u][v] = siz++; } u=ch[u][v]; } } int aks(int c[]) { int u=0; for(int i=1;i<=c[0];i++) { if(ch[u][c[i]]==0) return -1; u=ch[u][c[i]]; } return sum[u]; } }trie; int a[60],b[60],c[60],d[60]; int main() { mem(a),mem(b),mem(c); trie.init(); a[0]=a[1]=1; trie.insertt(a,0); b[0]=b[1]=1; trie.insertt(b,1); for(int i=2;i<100000;i++) { int len=b[0]; if(len>60) { for(int j=1;j<a[0];j++) a[j]=a[j+1];a[a[0]]=0;a[0]--; for(int j=1;j<b[0];j++) b[j]=b[j+1];b[b[0]]=0;b[0]--; len--; } //for(int j=0;j<100;j++)c[j]=0; len=max(a[0],b[0]); for(int j=1;j<=len;j++) c[j]=a[j]+b[j]; for(int j=1;j<len;j++) if(c[j]>9) c[j+1]++,c[j]=c[j]%10; if(c[len]>9) c[len]%=10,c[len+1]=1,len++; c[0]=len; int h=0; for(int j=c[0];j>=1;j--) d[++h]=c[j]; d[0]=c[0]; trie.insertt(d,i); for(int j=0;j<=b[0];j++) a[j]=b[j]; for(int j=0;j<=c[0];j++) b[j]=c[j]; } int T=read(); int oo=1; while(T--) { char s[60]; scanf("%s",s); int tmp[100]; for(int i=0;i<strlen(s);i++) tmp[i+1]=s[i]-'0'; tmp[0]=strlen(s); printf("Case #%d: %d\n",oo++,trie.aks(tmp)); } return 0; }