Summer Vacation

5

4 votes
Dynamic Programming and Bit Masking, Algorithms, Dynamic Programming
Problem

Alex is planning to do some projects during his summer vacation. There are N number of projects divided into 3 groups. Each project has two parameters - t[i], meaning the number of days required to complete that project, and g[i], meaning the group to which that project belongs. Now the summer vacation will last for D days, and Alex has to choose the projects so that the sum of the number of days required to complete the projects is exactly D. But there are two additional conditions -

  1. He will do each project only once.
  2. He will not do projects from the same group successively, i.e., no two adjacent projects can belong to the same group.

Now, Alex is thinking about the number of ways in which he can select the projects(their order matters) such that the sum of the time required to complete the projects is exactly D, and there are no two adjacent projects from the same group and each project is chosen at most once. Alex is busy preparing for the projects, can you help him to find the number of possible ways. Since, the answer can be very large, output it modulo 109+7.

Input Format:

  • The first line will contain a single integer T denoting the number of test cases. Then the test cases follows.
  • The first line of each test case will contain two space-separated integers N and D, denoting the total number of available projects and the number of days in the vacation.
  • The following N lines of each test case will contain two space-separated integers t[i] and g[i] denoting the number of days required to complete that project and the group to which the project belongs.

Output Format:

Print a single integer denoting the total number of ways of choosing the projects satisfying the given conditions modulo 109+7.

Constraints:

1<=T<=10

1<=N<=15

1<=D<=225

1<=t[i]<=15

0<=g[i]<=2

It is guaranteed that the sum of N over all test cases doesn’t exceed 15.

Sample Input
3
3 4
2 0
2 1
4 0
2 3
1 0
2 1
2 1
2 0
3 2
Sample Output
3
2
0
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For the first test case:

N = 3, D = 4

t1 = 2, g1 = 0

t2 = 2, g2 = 1

t3 = 4, g3 = 0

There are three possible combinations (1,2), (2,1) and (3) where the sum of the duration of the projects is equal to D which is 4 and the adjacent projects belong to the different groups. Hence, the output is 3.

For the second test case, there are two possible ways to choose the projects - (1, 2) and (2, 1). In both cases, the total number of days will be (1 + 2), i.e. 3, and the adjacent projects belong to the different groups. Hence, the output is 2.

For the third test case, there are no possible combinations since the duration of each project is greater than D. Hence, the output is 0.

Editor Image

?