
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains five space-separated integers,’N’, ‘R’, ‘B’, ‘X’ and ’Y’ denoting the total number of houses, number of red paint buckets, number of blue paint buckets, maximum number of consecutive houses that can be painted red and maximum number of consecutive houses that can be painted blue.
For each test case, print a single integer denoting the number of ways you can paint the houses modulo (10^8).
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 10
2 <= N <= 200
1 <= R, B <= 100
1 <= X, Y <= 10
Time Limit: 1 sec
We need to count all the possible combinations of colouring the houses such that no more than ‘X’ consecutive houses are painted red, and no more than ‘Y’ consecutive houses are painted blue.
We will be calculating this recursively.
Let F(p,q,t,s) denote the number of ways to colour ‘p’ + ‘q’ number of houses such that exactly ‘p’ number of houses are coloured red, exactly ‘q’ number of houses are coloured blue. Such that the next ‘t’ number of houses will strictly be red and next ‘s’ number of houses will strictly be blue. As we know, a house can only be coloured either red or blue, so either ‘t’ or ‘s’ will be 0.
For example :
In the above example F(3, 1, 2, 0) represents the number of ways to colour houses from 1 to 4 such that exactly 3 houses have colour red, 1 house has colour blue, and next 2 houses, i.e. houses from 5 to 6 are coloured red, and next 0 houses are coloured blue.
Few points to note :
After considering all the above points, we will have two options: place red at the end or blue at the end.
Combining these two we get :
F(p, q, t, s) = F(p - 1, q, t + 1, 0) + F(p, q - 1, 0 , s + 1)
Our final aim is to find the number of ways to arrange all the ‘N’ houses using ‘R’ red hoses, ‘B’ blue houses such that the next 0 houses have the colour red or blue. So the final answer is going to be :
F(R, B, 0, 0)
Notice that we will end up calling the function with the same values multiple times, for which we will end up recalculating the value which we already calculated.
Thus, there will be many overlapping subproblems in the recursive approach. This can be solved using the memoization technique.
We will use an array ‘dp’ to store an already visited state, and once we reach that state again, instead of recalculating its value, we will directly return the value we stored in the array ‘dp’.