This page is still under construction.

Parts of this page are still being built. What you see may change.

Combinations

Interview

Time limit2sMemory limit512 MB

Summary
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 kk elements out of a set of nn elements gives a kk-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 kk-combinations of a set of nn elements comes from this formula:

(nk)=n(n−1)⋯(n−k+1)k(k−1)⋯1\binom{n}{k} = \frac{n(n-1)\cdots(n-k+1)}{k(k-1)\cdots 1}

The list above gives (50)=1\binom{5}{0} = 1, (51)=5\binom{5}{1} = 5, (52)=10\binom{5}{2} = 10, (53)=10\binom{5}{3} = 10, (54)=5\binom{5}{4} = 5, (55)=1\binom{5}{5} = 1.

Given several (n,k)(n, k) pairs, compute (nk)\binom{n}{k} for each one.

Input

The first line has an integer tt. Each of the next tt lines has two integers nn and kk separated by a space.

  • 1≤t≤10001 \le t \le 1000
  • 1≤n≤10001 \le n \le 1000
  • 0≤k≤n0 \le k \le n

Output

For each (n,k)(n, k) pair, print the number of kk-combinations of a set of nn elements modulo 10000000071000000007 (109+710^9 + 7), one per line, in the order the pairs are given.

Examples3

  1. Example 1

    Input
    6
    5 0
    5 1
    5 2
    5 3
    5 4
    5 5
    
    Expected output
    1
    5
    10
    10
    5
    1
    
  2. Example 2

    Input
    3
    123 54
    7 4
    20 10
    
    Expected output
    757228090
    35
    184756
    
  3. Example 3

    Input
    2
    1 0
    1 1
    
    Expected output
    1
    1