# GOLDMINE - Editorial

#PROBLEM LINK:
[Contest][222]
**Author:** [Aman Kumar Gupta][4444]
**Editorialist:** [Aman Kumar Gupta][6666]
#DIFFICULTY:
EASY-MEDIUM.
#PREREQUISITES:
DP, Math, Matrix, Fast exponentiation.
#PROBLEM:
The number of ways you can mine a row of size <b>1 * n</b> using given types of bombs modulo <b>10^9+7</b>.
#EXPLANATION:
The first method which can be used is DP of course. It seems to be a classical DP problem.
So when the size of the row is 1 * 1, dp[1] = 0. When the size of the row is 1 * 2, dp[2] = 1 and similarly dp[3] = 1, where dp[i] refers to the ways in which the row of size <b>i</b> can be mined.
And from here upon, build the dp array as: dp[i] = (dp[i - 2] + dp[i - 3]) % 1000000007;
Finally dp[n] would be our answer.
But this will fail here because of the large constraint.
Now, suppose we have an equation like this: ~~<b>[dp[1], ~~<b>[dp[0], dp[1], dp[2]] * M = [dp[1], dp[2], ~~dp[3]] * M = [dp[2], dp[3], dp[4]]</b>, ~~dp[3]]</b>, where <b>M</b> is a matrix of size 3 * 3, then we can say ~~<b>[dp[1], dp[2], dp[3]] ~~<b>[dp[0], dp[1], dp[2]] * M^2 = ~~[dp[3], dp[4], dp[5]]</b>.
~~[dp[2], dp[3], dp[4]]</b>.
On generalizing, ~~<b>[dp[1], dp[2], dp[3]] ~~<b>[dp[0], dp[1], dp[2]] * M^(N - ~~3) ~~2) = [dp[n - 2], dp[n - 1], dp[n]]</b> for <b>N > 3</b>
To find the matrix, consider the matrix cells unknown and multiply it with the dp array on the left and equate it with the RHS. Leaving the task upon you, the matrix will be:<br>
0 0 1<br>
1 0 1<br>
0 1 0<br>
Now <b>M^(n - ~~3)</b> ~~2)</b> can be found in <b>27 * log n</b> and after all the multiplications of the above equation, our answer will simply be <b>FinalArray[2] (the last element of our resulting array on the RHS).</b><br><br>
<b>Note:</b> You need to take care of taking modulo on every operation to avoid the overflow. See the solution for more references.
#AUTHOR'S SOLUTION:
Author's solution can be found [here][333].
[4444]: http://www.codechef.com/users/aman_58472
[6666]: http://www.codechef.com/users/aman_58472
[222]: https://www.codechef.com/BULE2018/problems/GOLDMINE