REVFIB - Revenge of Fibonacci

no tags 

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.


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, 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.



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

This problem is first solved by team Y.E.S (Tsinghua University) at 77 minutes after the onsite contest starts. (They have 2 wrong tries before they get Accepted.)


hide comments
P[_]/|/K: 2013-06-26 15:34:10

this problem made my day!!!
@Blue Mary HATS OFF!!

(Tjandra Satria Gunawan)(曾毅昆): 2013-01-31 07:17:19

yeah! problem that seems impossible for me 3 months ago finally solved! huge precomputation needed... now I'll try to optimize the constant :-D
EDIT: First solver that using C(without plus plus) language ;-)

Last edit: 2013-01-31 07:24:33
[Rampage] Blue.Mary: 2011-12-14 12:48:51

It seems the sample makes that clear enough.

Aditya Muttur: 2011-12-14 10:23:44

what do you mean by smallest index?
when the fibonacci number is Fn, that starts with the same digits as those input, i need to find the smallest n right?

Added by:Fudan University Problem Setters
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:ACM/ICPC Regional Contest, Shanghai 2011; Problem Setter: Blue Mary