The new mayor of your city has appointed you to paint the houses of your city to make the city look beautiful. The city consists of βNβ houses arranged in a straight line.
The mayor has provided you with βRβ buckets of red paint and βBβ buckets of blue paint (R + B = N). And you need exactly 1 bucket to paint 1 house.
The mayor considers the city beautiful if no more than βXβ consecutive houses are painted red, and no more than βYβ consecutive houses are painted blue.
Apart from being a painter, you are also a good mathematician. Hence, you wonder how many ways you could paint the houses such that the city looks beautiful. Can you find the answer modulo 10^8?
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.
Output Format:
For each test case, print a single integer denoting the number of ways you can paint the houses modulo (10^8).
Note:
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
2
3 2 1 1 5
7 2 5 1 1
1
0
In the first test case, the only way to colour is red, blue, red.
In the second test case, we have 2 buckets of red and 5 buckets of blue. Hence, there is no possible way in which no two consecutive houses are painted blue.
2
5 1 4 1 1
2 1 1 4 5
0
2
Can we check all possible combinations of colouring the houses and count the beautiful ones?
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)
Algorithm:
O(2 ^ N), where βNβ is the total number of houses.
For each index, we are finding the number of ways to place both red and blue. So total time complexity becomes O(2 ^ N).
O(2 ^ N), where βNβ is the total number of houses.
Since we are going to call the recursive function at most 2 ^ βNβ times which will occupy space in the stack.So total space complexity becomes O(2 ^ N).