UVA

 The well-known Fibonacci sequence is defined as following:

        F(0) = F(1) = 1

        F(n) = F(n − 1) + F(n − 2) ∀n ≥ 2

 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.

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

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 struct Node{
 4     int id;
 5     Node * next[10];
 6         Node(){
 7         id = -1;
 8         for(int i = 0; i < 10; ++i)
 9             next[i] = NULL;
10     }
11 };
12 char Fib[50], In[50];
13 int F[2][1024000];
14 Node * const root = new Node();
15 void add_node(char *str, int id)
16 {
17     Node * u = root;
18     for(int i = 0, len = strlen(str); i < len && i <= 40; ++i){
19         int v = str[i] - '0';
20         if(!u->next[v])
21             u->next[v] = new Node();
22         u =  u->next[v];
23         if(u->id == -1)
24             u->id = id;
25     }
26 }
27 int query(char *str)
28 {
29     Node * u = root;
30     for(size_t i = 0, len = strlen(str); i < len; ++i){
31         u = u->next[str[i]-'0'];
32         if(!u) return -1;
33     }
34     return u->id;
35 }
36 void init()
37 {
38     memset(F, 0, sizeof(F));
39     F[0][0] = F[1][0] = 1;
40     int s = 0, e = 1;
41     add_node((char *)"1", 0);
42     add_node((char *)"1", 1);
43     for(int i = 2; i < 100000; ++i){
44         int p = i%2, q = (i+1)%2;
45         for(int j = s; j < e; ++j)
46             F[p][j] = F[p][j] + F[q][j];
47         for(int j = s; j < e; ++j)
48             if(F[p][j]>=10){
49                  F[p][j] %= 10;
50                  F[p][j+1] += 1;
51             }
52         if(F[p][e])  ++e;
53         if(e - s > 50)  ++s;
54         int r = e - 1, cnt = 0;
55         memset(Fib, 0, sizeof(Fib));
56         while(r >= 0 && cnt<45)
57             Fib[cnt++] = F[p][r--] + '0';
58         add_node(Fib, i);
59     }
60 }
61 int main()
62 {
63     ios::sync_with_stdio(false);
64     init();
65     int T; cin >> T;
66     for(int i = 1; i <= T; ++i){
67         cin >> In;
68         printf("Case #%d: %d
", i, query(In));
69     }
70     return 0;
71 }
原文地址:https://www.cnblogs.com/wydxry/p/7248983.html