Binomial coefficients

The binomial coefficient or choose function (nk)\binom{n}{k} is the number of ways to choose kk objects from nn objects. Its formula is well known: (nk)=n!k!(nk)!,\binom{n}{k} = \frac{ n! }{ k! (n-k)! } \enspace , where n!=n(n1)21n!=n\cdot (n-1)\cdots 2\cdot 1. This formula is not very useful from a computational point of view, because we have to deal with huge numbers (the factorial numbers) to obtain much smaller results. For instance,

(2010)=20!10!10!=24329020081766400001316819440000=184756.\binom{20}{10} = \frac{ 20! }{10! 10!} = \frac{2432902008176640000}{1316819440000} = 184756 \enspace . Despite the fact that the final number has only 6 digits, we need to compute 20!20!, which has 19 digits. This can be a problem because the type int of 32 bits cannot store numbers with more than 10 digits.

However, this is not the only way to compute (nk)\binom{n}{k}. For instance, binomial coefficients satisfy the following property: (nk)={1if k=0 or k=n(n1k1)+(n1k)if 0<k<n\binom{n}{k} = \left\{ \begin{array}{ll} 1 & \mbox{if $k = 0$ or $k = n$} \\ \binom{n-1}{k-1} + \binom{n-1}{k} & \mbox{if $0<k<n$} \end{array} \right. This recursive formula allow us to compute binomial coefficients with no multiplications nor divisions, by using a procedure known nowadays as “Pascal’s triangle” or “Tartaglia’s triangle”, although it has historical references more than 1000 years old:

11
11 11
11 22 11
11 33 33 11
11 44 66 11 11

\qquad

(00)\binom{0}{0}
(10)\binom{1}{0} (11)\binom{1}{1}
(20)\binom{2}{0} (21)\binom{2}{1} (22)\binom{2}{2}
(30)\binom{3}{0} (31)\binom{3}{1} (32)\binom{3}{2} (33)\binom{3}{3}
(40)\binom{4}{0} (41)\binom{4}{1} (42)\binom{4}{2} (43)\binom{4}{3} (44)\binom{4}{4}

To compute more binomial coefficients, you only have to fill more rows of the triangle. Use this idea to compute the value of several binomial coefficients.

Input

Input consists of several cases, each with two natural numbers nn and kk, where 0n300\le n\le 30 and 0kn0\le k\le n.

Output

For each case, print (nk)\binom{n}{k}.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T12:02:40.652Z

© Jutge.org, 2006–2026.
https://jutge.org