# UVa 530: Binomial Showdown

Problem 530 at the UVa Online Judge, also known as Binomial Showdown, asks us to compute the number of ways one can choose $k$ elements out of $n$ elements, not taking order into account. The problem is a classical one ($n$ choose $k$, anyone?), but with a twist. The input numbers are big, and we need to be really careful not to overflow integers in our intermediate computations. We discover a formula that has smaller intermediate values than the direct formula, and with one more small observation, we’re done.

## Interpretation

We are given two integers, $n$ and $k$, where $n \ge 1$ and $1 \le k \le n$. It is not explicitly stated how big $n$ can be, but it is likely safe to assume it fits in a 32-bit integer (but as a general rule: don’t assume anything). The problem statement tells us to compute the number of ways one can choose $k$ elements out of $n$ elements, not taking order into account. The problem statement is of course talking about the binomial coefficients, which most people recognise as $n$ choose $k$, $\binom{n}{k}$, $C(n, k)$ or $^nC_k$.

The problem setter is very kind to us this time. He tells us that the result will fit into a 32-bit integer, but warns us that intermediate results may or may not fit into a 32-bit integer, all depending on the algorithm used. That most likely means that the straight-forward solution will not work, but we are still just exploring the problem, so that’s where we’ll start anyway.

## Implementation

Everyone should remember the basic formula for the binomial coefficients, but in case someone has forgot, here it is:

$\displaystyle \binom{n}{k} = \frac{n!}{k! (n - k)!}$

Let’s use this formula to get a basic solution to test (we’ll also use long integers, just in case):

// computes n!
public static long Factorial(int n) {

long res = 1;

// n! = 1 * 2 * 3 * … * (n – 1) * n
for (int i = 1; i <= n; i++) { res *= i; } return res; } // computes n choose k public static long Choose(int n, int k) { // n choose k = n! / (k! * (n - k)!) return Factorial(n) / (Factorial(k) * Factorial(n - k)); } // the main method public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out, true); // read every test case while (true) { // read n and k int n = in.nextInt(), k = in.nextInt(); // if n = 0 and k = 0, then we are supposed to stop if (n == 0 && k == 0) { break; } // print n choose k out.println(Choose(n, k)); } }[/java] We can try the example test data on our current solution. We will find out that the solution works for the first two test cases, but the last one is wrong. Why? Because, like the problem setter warned us, intermediate computations are overflowing integers (and in this case, they're even overflowing long integers). Where is the overflow happening? If you look closely, you can see that we are computing $49!$ (at least in the last test case), and that is a huge number. Let's explore the formula to see if we can't somehow get rid of computing the whole factorial at once. Let's start by expanding the factorials:

$\displaystyle \frac{n!}{k! (n - k)!} = \frac{1 \times 2 \times \cdots \times (n - 1) \times n}{1 \times 2 \times \cdots \times (k - 1) \times k \times 1 \times 2 \times \cdots \times (n - k - 1) \times (n - k)}$

If we then cancel common factors from the top $n!$ and the bottom $(n - k)!$, we get:

$\displaystyle \frac{n!}{k! (n - k)!} = \frac{(n - k + 1) \times (n - k + 2) \times \cdots \times (n - 1) \times n}{1 \times 2 \times \cdots \times (k - 1) \times k}$

We can rewrite this as the following programmer-friendly product:

$\displaystyle \frac{n!}{k! (n - k)!} = \prod_{i = 1}^k \frac{n - k + i}{i}$

What’s good about this formula is that it removes unnecessary factors from the computation, resulting in smaller intermediate computations. This product can easily be implemented with a for-loop. We’ll just get rid of the Factorial method and modify our Choose function.

// computes n choose k
public static long Choose(int n, int k) {

long res = 1;

// n choose k = the product of (n – k + i) / i for i = 1..k
for (int i = 1; i <= k; i++) { res = res * (n - k + i) / i; } return res; }[/java] If we now try the test cases provided in the problem, we get all of them correct. Yay for us! Are we done yet? Well, almost. There is one tiny thing we have left to consider. Analyzing algorithms is a vital skill for any programmer that needs to work with algorithms, which is every programmer. If we analyze the running time of the Choose function, we find that it runs in $\theta(k)$ time, or time proportional to $k$. That means that, as $k$ gets larger, the algorithm needs more time to finish. In our scenario, $k$ can be as big as $n$, and $n$ is any valid positive 32-bit integer (so $1 \le n \le 2^{31} - 1)$. $1000000000$ choose $1000000000$ is equal to $1$, so it’s a perfectly valid input. If we feed this input to our current solution, it takes way too long to compute the result (more than a couple of seconds on my computer). So how can we fix this? One identity of the binomial coefficients is the following:

$\displaystyle \binom{n}{k} = \binom{n}{n - k}$

It can easily be derived:

$\displaystyle\binom{n}{n - k} = \frac{n!}{(n - k)! (n - (n - k))!} =$
$\displaystyle \frac{n!}{(n - k)! (k)!} = \binom{n}{k}$

This is beneficial to us, because now we can either compute $n$ choose $k$ or $n$ choose $(n - k)$, and still get the same result. It takes less time to compute the smaller of $k$ and $(n - k)$, so that’s the one we’ll pick. Only minor modifications are needed to implement this.

// computes n choose k
public static long Choose(int n, int k) {

// n choose k is equal to n choose (n – k),
// so we pick the one that is easier to compute,
// which is the smaller one
k = Math.min(k, n – k);

long res = 1;

// n choose k = the product of (n – k + i) / i for i = 1..k
for (int i = 1; i <= k; i++) { res = res * (n - k + i) / i; } return res; }[/java] Now the solution is fast enough to get an Accepted verdict at the online judge.

## Conclusion

Today’s problem was not complicated, but required clever optimizations to avoid a Time limit exceeded verdict.

You can take a look at the complete source code here.

### Posted by Bjarki Ágúst

atul

to calculate c(n,k) . we can use DP to solve it in efficient manner

Below code actually forms pascal triangle and return the result accordingly.

int computeBinomial(int n, int k)
{
int* P = (int*)calloc(k+1, sizeof(int));
int j,i;

P[0] = 1;

for(i = 1; i 0; j–)
{
P[j] = P[j] + P[j-1];
}
}

return P[K];
}

Bjarki Ágúst

Your code got a little messed up, but here is what I imagine it was supposed to look like (in C++):

int nck2(int n, int k)
{
k = min(k, n - k);
int* P = new int[k + 1];
for (int i = 0; i <= k; i++) P[i] = 1;

for (int i = 2; i <= n; i++)
{
for (int j = min(k, i - 1); j >= 1; j--)
{
P[j] += P[j - 1];
}
}

int res = P[k];
delete[] P;
return res;
}

Let me know if it is not what you had in mind.

Let’s analyze the code a bit. It runs in $O(nk)$ time, and needs $O(k)$ memory. Because $n$ and $k$ can get so big ($n$ can go up to $2^{31}$ and $k$ up to $2^{30}$), this code just is not going to work. It will both be too slow, and take too much space. There are two upsides to this code though. First, it only uses addition which may be useful, and second, the intermediate results will never be bigger than the actual result.

But thanks for the comment and the suggestion, even though the code will get a Time limit exceeded verdict at the online judge.

snnn

UVA 369 is similar as this problem , almost the same one !