AMIFIB - Editorial

PROBLEM LINK:

Practice
Contest

Author: Vineet Paliwal
Tester: Roman Rubanenko
Editorialist: Jingbo Shang

DIFFICULTY:

Easy

PREREQUISITES:

Fibonacci Property, Quick Sort, Offline Algorithm, Hash, Binary Search

PROBLEM:

Given T big integers with at most D digits, determine whether it can be a fibonacci number. The total digits are L.

EXPLANATION:

There are a number of ways to solve this problem.

If you look up fibonacci properties online, you may find the followings:
n is a fibonacci number if and only if 5 * n * n + 4 or 5 * n * n - 4 is a perfect square number.
Using this property, together with big integer multiplication and sqrt, you can get the answer.

However, this method is too complex to pass this problem. It is worth noting that fibonacci number increases exponentially. And thus, there are only O(D) fibonacci numbers have at most D digits. This observation gives us the intuition to solve this problem much simpler.

The fist method is an offline version. First, we load all queries and sorted them. This step will take O(TDlogT) if we use quick sort. Second, we compute fibonacci numbers one after another using big integer addition. For each fibonacci number, check its relationship with the current smallest query number:

If the they are same, then the answer of that query is YES and letā€™s look at the next query number;
If the fibonacci number is smaller, then letā€™s look at the next fibonacci number;
if the fibonacci number is larger, then the answer of that query is NO and letā€™s look at the next query number.

This procedure needs O((D + T)D) time. Therefore, this offline versionā€™s time complexity is O(TDlogT + (D + T)D).

The second one involves hash. We can simply generate O(D) fibonacci number and only restore the remainder of some relatively big number, for example, 2^64 or 10^9+7. And then, check the query numberā€™s remainder to see whether it occurred using hash table or tree set. Suppose we use hash table, the time complexity is O(D + L).

AUTHORā€™S AND TESTERā€™S SOLUTIONS:

Authorā€™s solution can be found here.
Testerā€™s solution can be found here.

3 Likes

I used fast doubling method to compute all Fibonacci numbers uptil the 4800th Fibonacci number, because it has 1003 digits(just greater than 1000 digits) and then just checked if the query occurred in the computed list.

In fast doubling, using f(n) and f(n+1) we can compute f(2n) and f(2n+1).

f(2n) = f(n)(2f(n+1) - f(n))

f(2n+1) = (f(n+1))^2 + (f(n))^2

Click here to see my solution in Python.

9 Likes

I got AC using the property that if n is a fibonacci number then 5 * n * n + 4 or 5 * n * n - 4 is a perfect square number. CodeChef: Practical coding for everyone

2 Likes

This question was biased towards python, java and other such languages. I donā€™t think there were many C/C++ users who solved this without the BIGINT template and those who did would have suffered greater time penalty. In my opinion the easiest question of the contest should not be language dependent.

22 Likes

Used arrays to store Fibonacci numbers.Each digit stored in different index. Somewhat similar approach as given in the tutorial for small factorials practice(easy). Worked fine.

test cases were weak

check the test case and solution given on this link

3 Likes

The input was not properly formatted. It cost me 3 penalties in python. I finally used int(raw_input()) instead of input() to scan integers and it got accepted :expressionless:

9 Likes

Testerā€™s Solution uses a unsigned long long for a the number which is 1000 digits long!

Someone please explain how its working!

ALL SEE TESTERā€™s SOLUTION ONCE!

4 Likes

Cant understand how the testerā€™s solution works for big numbersā€¦can any1 explain?

1 Like

Hello @all,

I too attempted to solve this question in Python using the idea described in the first paragraph of the editorial and got multiple NZEC and WA veredictsā€¦ And I have to admit that precomputing the values up to a large limit never occurred me during the contestā€¦

Can anyone just clear me if the test cases were well designed and there are actually numbers with up to 1000 digits there?

Also, Iā€™m assuming that the testerā€™s solutions works since it implicitly uses the property of when an overflow occurs the value is ā€œautomaticallyā€ wrapped around the range of an ULL and this would serve as a (possibly very cumbersome) hashing technique?

Because if such trick doesnā€™t apply here then something was definitely wrong with the test cases I thinkā€¦

Best,

Bruno

We can simply generate O(D) fibonacci number and only restore the remainder of some relatively big number, for example, 2^64 or 10^9+7.

I donā€™t understand this part. Are we storing the Fibonacci numbers or number % 10^9 + 7 ?

The testcases are weak. People who did the problem with unsigned long long int also passed the testcases. Basically, according to the testcases, storing fibonacci numbers MOD 2^64 would have passed all the test cases !

2 Likes

@all : I am the setter for this problem .

You could generate all fibonacci numbers till 1000 digits ( no modulo is needed like the tester did ) and store them . The time limit and memory limit was conducive to do such a computation . You could store them in an array and do binary search for solving each query .

Or , otherwise you could use the fibonacci number property that 5nn + 4 or 5nn - 4 or both are perfect square to solve it . My solution ( setterā€™s solution ) uses it .

There were indeed test cases which had near about 1000 digits .

3 Likes

Whatā€™ the problem with my solution??? It was not accepted but I compared the output with others solutions which are accepted. I took large numbers (like 354224848179261915075 fib(100)) alsoā€¦ My code works fineā€¦ but when I submitted,its giving wrong answerā€¦ CodeChef: Practical coding for everyoneā€¦ Please help meā€¦

My solution is perfectā€¦ I ran the testerā€™s solution and it starts generating wrong fibonacci numbers after F(94)ā€¦ So my solution was not get acceptedā€¦ Testerā€™s F(95) - 1293530146158671551ā€¦ My F(95) - 19740274219868223167ā€¦

@all : Tester is not storing big numbers . He is storing number mod 2^64-1 . Since numbers overflow by themselves he is not doing modulo operation . While this approach may pass , this cannot guarantee correctness , and I am not surprised that it fails some of the test cases that users are talking about .

4 Likes

@kurumua : I made the problem statement and test cases . The tester wrote and submitted his code after the test cases were posted . I discussed with the tester that ā€œhashingā€ on any particular ā€œmodulusā€ canā€™t guarantee correctness . However since the output for the test cases was generated by my solution , the correctness was guaranteed . And we could not have prevented all possible ā€œmodulusā€ for hash from passing the test cases .

@kuruma : I agree that giving the ā€œhashingā€ based solution to ā€œeditorialistā€ as a reference solution is highly misleading especially beginners . If you want you can write to codechef adminā€™s at bugs@codechef.com to get the ā€œtesterā€™sā€ solution replaced . I had already raised this issue during the problem setting process .

2 Likes

@vineetpaliwal, thanks for your reply and cofirmation :slight_smile:

I will definitely e-mail codechef admins regarding this matter as soon as possible for me :smiley:

By the way, the problem was interesting and your idea for solving it was also very good :slight_smile:

Best,

Bruno

1 Like

I ran the testerā€™s code on my system, I am very sad to see that the output is ā€˜YESā€™ for all numbers greater than 10^64 :frowning:

1 Like

what is wrong in my solution ??