2013
11-11

# Binomial Showdown

In how many ways can you choose k elements out of n elements, not taking order into account?

Write a program to compute this number.

The input will contain one or more test cases.

Each test case consists of one line containing two integers n (n>=1) and k (0<=k<=n).

Input is terminated by two zeroes for n and k.

For each test case, print one line containing the required number. This number will always fit into an integer, i.e. it will be less than 231.

Warning: Don’t underestimate the problem. The result will fit into an integer – but if all intermediate results arising during the computation will also fit into an integer depends on your algorithm. The test cases will go to the limit.

4 2
10 5
49 6
0 0


6
252
13983816


//* @author  mekarlos@gmail.com
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
static long[][] M=new long[2000][2000];
public static long comb(int n,int k){
if(k>(n/2))k=n-k;
if(n< 2000&&k< 2000) return M[n][k];
else if(k==n||k==0) return 1;
else if(k==1) return n;
else return comb(n-1,k)+comb(n-1,k-1);
}
public static void main(String[] args) throws IOException {
StringTokenizer token;
int n=0,k=0;
for(int i=0;i< 2000;i++){
M[i][i]=1;
M[i][0]=1;
M[i][1]=i;
M[0][i]=0;
}
for(int i=1;i< 2000;i++){
for(int j=2;j< 2000;j++){
if(i!=j){
M[i][j]=M[i-1][j-1]+M[i-1][j];
}
}
}
while(true){
}