• + 0 comments

    In my opinion, the editorial is needlessly complex. You can get the result more intuitively by using matrix exponentiation alone -- no need for dynamic programming or additional multiplications as seen in the editorial.

    You'll of course have to find the transformation matrix, T. This is quite straightforward, especially if you do it in the usual way:

    T[0] | F(n)
    T[1] | F(n-1)
    T[2] | F(n-2)
    etc.
    

    Then the answer can be found like this:

    Matrix T = BuildMatrix();
    Matrix result = T ^ N;
    
    return result[0][0] * 2;
    

    This will be sufficient for every value of N.