Revenge of Fibonacci
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 204800/204800 K (Java/Others)
Total Submission(s): 3218 Accepted Submission(s): 821
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define ql(a) memset(a,0,sizeof(a))
#define LL long long
const int UP=;
const int N=-;
struct node
{
int val;
node *child[];
node(){val=-;for(int i=;i<;++i) child[i]=NULL;}
}*root;
void ins(char *s,int num)
{
node *p=root;
int minn=min(,(int)strlen(s));
for(int i=;i<minn;++i){
int t=s[i]-'';
if(p->child[t]==NULL){
p->child[t]=new node();
}
p=p->child[t];
if(p->val<) p->val=num;
}
}
void init()
{
int f1[],f2[],f3[],r=;
ql(f1),ql(f2),ql(f3);
ins("",);
f1[]=f1[]=f2[]=f2[]=;
for(int i=;i<=N;++i){ql(f3);r=;
int ml=max(f1[],f2[]);
for(int j=;j<=ml;j++){
f3[j]=f1[j]+f2[j]+r;
r=f3[j]/;
f3[j]%=;
if(j==ml&&r) ml++;
}f3[]=ml;
char s[]; ql(s);int l=;
for(int j=f3[];j>=;j--) s[l++]=f3[j]+'';
for(int j=;j<=f3[];j++) s[j-]='\0';
ins(s,i);
ql(f1); for(int j=;j<=f2[];j++) f1[j]=f2[j];
ql(f2); for(int j=;j<=f3[];j++) f2[j]=f3[j];
if(ml>){
for(int j=;j<f1[];j++) f1[j]=f1[j+]; f1[f1[]--]=;
for(int j=;j<f2[];j++) f2[j]=f2[j+]; f2[f2[]--]=;
}
}
}
int Find(char *s)
{
int len=strlen(s);
if(!strcmp(s,"")) {return ;}
node *p=root;
for(int i=;i<len;++i){
int t=s[i]-'';
if(p->child[t]==NULL) return -;
p=p->child[t];
}
return p->val;
}
int main()
{
int k,cas=;
char p[];
root=new node();
init();
cin>>k;
while(k--){
scanf("%s",p);
printf("Case #%d: %d\n",++cas,Find(p));
}
return ;
}