Binomial coefficients X29864


Statement
 

pdf   zip

The binomial coefficient (Nk)(N \;\; k), 0kN0\leq k\leq N, is an important concept in mathematics. Formally, (Nk)(N \;\; k) represents the number of ways to choose a subset of kk elements from a set of NN elements. For example, there are three ways to choose a subset of 22 elements from a set {a,b,c}\{a,b,c\} of three elements, namely {a,b}\{a,b\}, {a,c}\{a,c\} and {b,c}\{b,c\}. Hence (32)=3(3 \;\; 2) = 3.

To compute (Nk)(N \;\; k), it is convenient to use the following recursive formula: (Nk)=(N1k1)+(N1k).(N \;\; k) = (N-1 \;\; k-1) + (N-1 \;\; k). The base case given by (N0)=(NN)=1(N \;\; 0)=(N \;\; N)=1 for any N0N\geq 0.

The binomial coefficients can be arranged into Pascal’s triangle:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

Each row N0N\geq 0 contains the binomial coefficients (N0),,(NN)(N \;\; 0),\ldots,(N \;\; N), and each element is the sum of the two elements immediately above it.

Input

The input starts with an integer CC, the number of cases. On each of the following CC lines are two integers NN and kk satisfying 0kN200\leq k\leq N\leq 20.

Output

For each case, output the binomial coefficient (Nk)(N \;\; k) on a single line.

Public test cases
  • Input

    4
    0 0
    3 2
    4 4
    6 2
    

    Output

    1
    3
    1
    15
    
  • Information
    Author
    Anders Jonsson
    Language
    English
    Official solutions
    C++
    User solutions
    C++