#### Help Sam to find out the total number of ways he can play the game.
Input: ‘N’ = 4 , 'ENERGY' = [1, 3, 2, 9]
Output: 7
Explanation:
Array elements represent the ENERGY points of ith level.
9 => 2
9 => 2 => 1
9 => 3 => 1
9 => 1
2 => 1
3 => 1
1 (Sam can start and end at 1)
Thus, The total number of ways to play the game = 7
The first line of the input contains an integer, 'N’, denoting the size of the array (Number of ENERGY levels).
The second line of each test case contains ‘N’ space-separated integers denoting elements of the array (ENERGY Points of the levels).
Return an integer denoting the number of ways (Modulo 10^9 + 7).
1 <= T <= 10
1 <= N <= 500
1 <= ENERGY[i] <= 100
Time Limit: 1 sec
The solution for the problem lies in counting the number of strictly increasing subsequences whose gcd is 1.
We will maintain a 1D array ‘subseq’ , which stores the subsequence of the array. We start by writing a recursive code that fixes the elements in the array one by one and then generates all the subsets starting from them. After the recursive call, we erase the last element from the ‘subseq’ array to generate the next subsequence.
Our base condition would be returning as soon as we reach the last index, but before returning we also check whether the subsequence stored in ‘subseq’ array is strictly increasing or not, also we have to check whether the overall gcd is 1 or not. If both conditions are satisfied we can increase our counter as we got one of the possible ways.
The steps are as follows :
We can observe that the gcd of any subsequence would be from 1 to 100 as the maximum value for the array element is 100. So the idea of the solution would be to store the count of the number of strictly increasing subsequences that are ending at the current index for every value of gcd.
States: 2 State DP - index and gcd.
DP[ idx ][ gc ] = Number of strictly increasing subsequences ending at the index = ‘idx’ with gcd = ‘gc’ .
Transition:Let ‘idxBefore’ be any index lesser than the current index ‘idx’ having an element value smaller than the element value at the current index, so the transition would look like this:
DP[ idx ][ gcd(array value at idx , gc) ] += DP[ idxBefore ][ gc ]
For every value of ‘gc’ in range [1,100].
Assign the DP table with 0 initially.