FIBEASY - Unofficial Editorial






Fibonacci Sequences.


Initially we are given an array A=[a_{1}, a_{2}, a_{3}....,a_{N}]. We are supposed to remove all elements occurring at odd indices in one step and we have to repeat this process until only single element remains. Let’s say we are left with some element a_{k}, so we need to output the last digit of the kth Fibonacci number.


Fibonacci numbers follow the property : F'(N) = (F'(N-1) + F'(N-2)) \mod 10.
Here F'(N). represents the last digit of Nth fib number. Now if we look at the terms F'(N) is composed of, we can see that the pattern of F'(N) will start repeating at most after 10*10 terms, i.e we will get a cycle.
A simple trick to find when the cycle starts would be to find when again in the array the first and second terms consecutively appear. In our case here, cycle size = 60.
Now let’s get to the second part of the problem. Basically, we’re dividing our array A repeatedly by 2. So we can make an observation that the number which would be the highest power of 2 in the array would remain until the end.


If we observe more closely, we can see that we’ll be only accessing these elements-
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048...
Since our cycle length is 60, we take the modulus of all these elements with 60 and we get-
1, 2, 4, 8, 16, 32, 4, 8, 16, 32, 4, 8...
We see that after the 1st and 2nd term, we have the repetition of 4th, 8th, 16th and 32nd term. So we only need to store these elements.


#include <iostream>
using namespace std;
typedef long long lld;

int log(lld N) {
    int count = 0;
    while (N) {
        N /= 2;
    return count-1;

int main() {

    // freopen("input.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    int dp[] = {2, 3, 0, 9};

    while (T--) {
        lld N;
        scanf("%lld", &N);
        int max_power = log(N);
        if (N==1) {
        } else if (N==2 || N==3) {
        } else {
            printf("%d\n", dp[(max_power-2) % 4]);
    return 0;


Time complexity of the algorithm discussed in this editorial is O(lg(N)) per test case.


same approach i tried but getting wrong answer:smirk:
here is my solution.plz tell me my mistakes.

Can anyone please tell what may be the problem with this solution because it follows similar approach as in the editorial (apart from the last part) but it fails for large test case somehow -

Don’t use C++ inbuilt log as I guess it gives some floating point errors. Create your own function to calculate max power 2 term like the one shown in editorial

Dont use inbuilt log :stuck_out_tongue:

1 Like

awwwwwwwww maaaannn :sob::sob::sob: can’t belive lost 80 points coz of inbuilt function nonetheless thank you very much for pointing out the mistake.

Inbuilt log function returns float or double so for sufficiently large value the return value is an approximation and not an integer and that on conversion to integer might be less than 1 then the actual value.


You can ultimately use the Fast Doubling Method to calculate Fibonacci Numbers under Modulo something in Log(N).

Solution using matrix exponential



1 Like

@gotham You seem to be a true programmer!! :sunglasses: Check this


Damn :joy::joy::joy:


my code is constantly showing runtime errors . i checked for the limits of vector i used for accessing the elements and found all of them fine i.e. i did not ask for any element beyond limit but it is still showing either SIGSEGV or SIGTSTP error. i am not able to find any other fault. one thing more, that, when i go for the custom input then it gives the correct output . kindly guide me to know the possible reason for this problem.

man you are legend:stuck_out_tongue_winking_eye::stuck_out_tongue_winking_eye:

I was almost near-see this

that not his code though ! . who submmited that was a legend :laughing:

1 Like

can anybody help!!
i don’t understand what is wrong is this approach… keep showing WA… please help

import math
def fib(n):
    if n in fib_cache:
        return fib_cache[n]
    if n == 1:
        value = 0
    elif n==2:
        value = 1
    else: value = (fib(n-1)+fib(n-2))
    return value
    test = int(input())
    for i in range(test):


I also type casted log and used it in my python code, and it gave me the same error,
Then i made the loop myself into a O(lgn) complexity one and submitted getting AC
I think for large values typecasting of log is somehow not giving the right response

thanks man, its good lesson to learn.

What’s wrong with this approach? I am passing one of test case of 30 points. But failing somewhere. My code is fast enough to not get TLE. Then whats wrong?