The first k bits of the Fibonacci series are given. k cannot exceed 40, and the smallest positive integer n is found. The first k bits of F (n) are the same as the first k bits of the given number, the number of items in the Fibonacci series cannot exceed 100000.
Analysis: This question can be divided into two steps:
The first step is to pre-process the first 40 digits of the 100000 Fibonacci series and insert them into the dictionary tree.
The second step is to query the matching result to obtain the minimum n.
For the first step, we can set the Fibonacci sequence to more than 50 digits, and save only 40 digits to prevent carry errors. In the addition Process of the Fibonacci series, we only put the first 50 or more
BITs are added, otherwise they cannot be saved.
#include <iostream>#include <string.h>#include <stdio.h>using namespace std;const int N=10;int f1[65],f2[65],f3[65];class Trie{ public: int v; int flag; Trie *next[N]; Trie() { v=-1; memset(next,NULL,sizeof(next)); }};Trie *root;void Insert(char *S,int ans){ int len=strlen(S); Trie *p=root; for(int i=0;i<len;i++) { int id=S[i]-'0'; if(p->next[id]==NULL) p->next[id]=new Trie(); p=p->next[id]; if(p->v<0) p->v=ans; }}int Find(char *S){ Trie *p=root; int count; int len=strlen(S); for(int i=0;i<len;i++) { int id=S[i]-'0'; p=p->next[id]; if(p==NULL) return -1; else count=p->v; } return count;}void Init(){ int h; char str[65]="1"; memset(f1,0,sizeof(f1)); memset(f2,0,sizeof(f2)); memset(f3,0,sizeof(f3)); f1[0]=1;f2[0]=1; Insert(str,0); for(int i=2;i<100000;i++) { memset(str,0,sizeof(str)); int r=0; for(int j=0;j<60;j++) { f3[j]=f1[j]+f2[j]+r; r=f3[j]/10; f3[j]%=10; } for(int j=59;j>=0;j--) if(f3[j]) { h=j; break; } int k=0; for(int j=h;j>=0;j--) { str[k++]=f3[j]+'0'; if(k>=40) break; } Insert(str,i); if(h>55) { for(int j=1;j<59;j++) f3[j-1]=f3[j]; for(int j=1;j<59;j++) f2[j-1]=f2[j]; } for(int j=0;j<60;j++) f1[j]=f2[j]; for(int j=0;j<60;j++) f2[j]=f3[j]; }}int main(){ root=new Trie(); Init(); char str[105]; int t,i,j,k=1; scanf("%d",&t); while(t--) { scanf("%s",str); printf("Case #%d: ",k++); int tmp=Find(str); printf("%d\n",tmp); } return 0;}