You have 'N' chocolates and 'K' empty bags. Your task is to pack all the chocolates in these bags such that any bag can have any number of chocolates, but no bag remains empty. Please note that all the chocolates are different, i.e., they can be numbered from 1 to 'N', but all bags are identical.
E.g., If there are 3 chocolates and 2 bags, then you can pack the chocolates in the following three ways -
[1, 2] and [3].
[2, 3] and [1].
[1, 3] and [2].
Only the above three packings are considered valid. Some more packages are possible; for example, packing [{2}, {3, 1}] is not taken because [{1, 3}, {2}] is already considered, i.e., order of bags and the order of chocolates inside a bag don't matter.
You are given the number of chocolates 'N' and the number of bags 'K'. Find the number of ways to pack the chocolates. Return your answer modulo 10^9+7.
The first line contains 'T', denoting the number of tests.
For each Test :
The only line contains two space-separated integers, 'N' and 'K', denoting the number of chocolates and bags, respectively.
Output Format:
For each test, print one integer, denoting the number of valid ways (modulo 10^9+7) to pack given 'N' chocolates into 'K' bags.
Note:
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= 'T' <= 10
1 <= 'K' <= 'N' <= 10^3
Time Limit: 1 sec
2
3 2
4 4
3
1
For case 1:
Three valid packagings are as follows:
Any one among [{1, 2}, {3}] , [{2, 1}, {3}] , [{3}, {1, 2}] and [{3}, {2, 1}], as all of these are considered same.
Any one among [{2, 3}, {1}] , [{3, 2}, {1}] , [{1}, {2, 3}] and [{1}, {3, 2}].
Any one among [{1, 3}, {2}] , [{3, 1}, {2}] , [{2}, {1, 3}] and [{2}, {3, 1}].
For Case 2:
The only way is to pack one chocolate in each bag.
3
3 1
5 2
6 3
1
15
90
Select all the chocolates one by one, and find all possible ways to pack them.
Approach: There are 'N' chocolates and 'K' bags; assume that you have solved N-1 chocolates and now solve N-th chocolate.
There will be two ways to pack this chocolate -
Merging both possibilities, Solution(N, K) = K * Solution(N-1, K) + Solution(N-1, K-1).
Repeat the same process for each chocolate, i.e., solve for i-th chocolate, assuming that you have a solution for (i-1) chocolates.
Algorithm:
O(2 ^ N), where 'N' is the number of chocolates.
There are two recursive calls for each of the 'N' chocolates. Hence overall time complexity becomes (2 ^ N).
O(N), where 'N' is the number of chocolates.
Apart from recursion, constant space is used. The maximum stack size for recursion on 'N' elements is O(N). Hence overall space complexity becomes O(N).