Combinations
InterviewTime limit2sMemory limit512 MB
Given up to 1000 pairs (n, k), compute the binomial coefficient C(n, k) modulo 10^9+7 for each pair.
- Level
Medium4 of 10
- Topics
- Combinatorics, Math, Number theory, Dynamic programming
- Solved
- No attempts yet
Problem
Taking elements out of a set of elements gives a -combination.
For the set of the numbers from 1 to 5, the combinations are these:
- 1-combinations (1 element at a time): (1), (2), (3), (4), (5)
- 2-combinations (2 elements at a time): (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)
- 3-combinations (3 elements at a time): (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5), (2, 3, 4), (2, 3, 5), (2, 4, 5), (3, 4, 5)
- 4-combinations (4 elements at a time): (1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)
- 5-combination (all elements at once): (1, 2, 3, 4, 5)
- 0-combination (no element): ()
The number of -combinations of a set of elements comes from this formula:
The list above gives , , , , , .
Given several pairs, compute for each one.
Input
The first line has an integer . Each of the next lines has two integers and separated by a space.
Output
For each pair, print the number of -combinations of a set of elements modulo (), one per line, in the order the pairs are given.