


Given ‘N’ = 3,
F(3) = 13,This is because F(0) = 1, F(1) = 1, Therefore F(2) = 2*F(1) + 3*F(1), therefore F(2) = 5, and F(3) = 2*F(2) + 3*F(1), therefore F(3) = 13.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines contains a single integer ‘N’, which denotes the number given to us.
For each test case, You are supposed to return the Nth term for the given relation.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10 ^ 9
Time Limit: 1sec.
The idea is to recur according to the given relation i.e. F(N) = 2*F(N - 1) + 3*F(N - 2).
The steps are as follows:
The idea is to maintain a ‘dp’ array where ‘dp[i]’ represents the ‘I’th term for the given relation, where ‘dp[i]’ = ‘2 * dp[i-1]’ + ‘3 * dp[i-2]’.
The steps are as follows:
The idea is to use Matrix Exponentiation to speed up the function computation.
All such problems where a term is a function of other terms in a linear fashion. Then these can be solved using the Matrix . First, we make a transformation matrix and then just use matrix exponentiation to find the Nth term. This can be done logarithmic time instead of linear time as seen in the first approach.
For matrix exponentiation we need:
We further multiply the Matrix ‘N’ times and do this in logarithmic time by following the logic:
In the solution, we are using a similar idea.
The steps are as follows: