

For n = 2, there are three cycle D -> A -> D, D -> B -> D and D -> C -> D
The first line of input contains an integer ‘T’, denoting the number of test cases. Then each test case follows.
The first line and the last line of each test case contain the integer ‘N’ denoting the required length of the cyclic path.
For each test case, print the count the number of ways in which the ant can go from the initial vertex ‘D’ to itself in exactly ‘n’ steps modulo 1000000007 (10^9 + 7).
The output of each test case will be printed on a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10 ^ 5
Time Limit: 1 sec.
Let’s call vertex ‘A’ as 3, ‘B’ as 2, ‘C’ as 1 and ‘D’ as 0.
The idea is to break the main problem into smaller subproblems and recursively solve the smaller subproblems.
Let P(x, n), represents the number of ways to move from vertex 0 to vertex x in ‘n’ steps.
Then our main problem is P(0, n).
P(0, n ) = P(1, n - 1) + P(2,n -1) + P( 3, n - 2).
Since vertex 1, 2 and 3 are equivalent to each other so we can write, P(0, n) = 3 * P(1, n -1)
And for x != 0 let's say 1.
P(1, n) = 2 * P(1, n - 1) + P(0, n - 1).
Hence we got the optimal substructure property.
The steps are as follows:
Let 'numberOfWays' be the function that count all the possible ways
This function returns the number of ways to reach vertex ‘0’ from vertex ‘vertex’ in ‘steps’.
Let’s call vertex ‘A’ as 3, ‘B’ as 2, ‘C’ as 1 and ‘D’ as 0.
The idea is to break the main problem into smaller subproblems and recursively solve the smaller subproblems.
Let P(x, n), represents the number of ways to move from vertex 0 to vertex x in ‘n’ steps.
Then our main problem is P(0, n).
P(0, n ) = P(1, n - 1) + P(2,n -1) + P( 3, n - 2)
Since vertex 1, 2 and 3 are equivalent to each other so we can write, P(0, n) = 3 * P(1, n -1)
And for x != 0 let's say 1.
P(1, n) = 2 * P(1, n - 1) + P(0, n - 1).
Hence we got the optimal substructure property. If we observe carefully the subproblems are repetitive. So we can use memozition to avoid recomputation of repetition subproblems.
The steps are as follows:
Let 'numberOfWays' be the function that count all the possible ways
This function returns the number of ways to reach vertex ‘0’ from vertex ‘vertex’ in ‘steps’.
If you observe carefully, you will see that the current state of ‘dp’ is only dependent upon the previous state of ‘dp’.
We will maintain two variable ‘waysZero’, denoting the the number of ways to reach vertex ‘0’ from ‘0’ in ‘n’ steps and second variable ‘waysNonZero’, denoting the the number of ways to reach vertex other than 0’ from ‘0 in ‘n’ steps.
waysZero = 1
waysNonZero = 0
Let P(x, n), represents the number of ways to move from vertex 0 to vertex ‘x’ in ‘n’ steps.
Then our main problem is P(0, n).
P(0, n ) = P(1, n - 1) + P(2,n -1) + P( 3, n - 2)
Since vertex 1, 2 and 3 are equivalent to each other so we can write, P(0, n) = 3 * P(1, n -1).
And for x != 0 let's say 1.
P(1, n) = 2 * P(1, n - 1) + P(0, n - 1)
waysZero = 3 * waysNonZero
waysNonZero = 2 * waysNonZero + waysZero
We can use a temporary variable to perform above transition.
Let 'numberOfWays' be the function that counts all the possible ways to reach from vertex 0 to 0 in exactly 'n' steps.