1.


import java.util.Scanner;

/**
* Angewandte Mathematik, SS11
* Problem: 369 - Combination
* Link: http://uva.onlinejudge.org/external/3/369.pdf
*
* @author Benedikt Zönnchen
* @author Erik Wenzel
* @version 1.0, 28/04/2011
*
* Method : Combinatorics, Memorizing
* Status : Accepted
* Runtime: 0.384
*
* Erklärung: Es gibt 2 Möglichkeiten.
* 1. Entweder man rechnet mit der Fakultät (n über k) = n!/(k!(n-k)!)
* dabei muss man aber nicht alle Fakultäten ausrechnen sondern kann schon
* davor kürzen. Bsp. (6 über 4) = 6!/(4!(6-4)!) = (6*5*4*3*2*1)/(4*3*2*1*2*1) = (6*5)/(2)
*
* 2. Man wendet die Rechenregeln für den Binoialkoeffizienten an: (n über k) = (n-1 über k) + (n-1 über k-1)
* und (n über n) = (n über 0) = (0 über k) = 1
* Mit (n über k) = (n über n-k) falls k>n spart man sich dann nochmal fast die Hälfte der nötigen Rechnungen.
*
* Wir haben die 2te Lösung implementiert!
*/

public class Main
{
static long[][] bins = new long[101][101];
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n,k;

while(true)
{
n = sc.nextInt();
k = sc.nextInt();
if(n==0 && k==0)
{
break;
}
System.out.println(n+" things taken "+k+" at a time is " + getBinomialCoefficient(n,k) + " exactly.");
}
}

/**
* Calculate the binomial coefficient n over k (use memorizing)
* @param n
* @param k
* @return return n over k
*/
public static long getBinomialCoefficient(int n, int k)
{
if(n == k || n == 0 || k == 0)
{
return 1;
}

if(k>n)
{
k=n-k;
}

if(bins[n][k]!=0)
{
return bins[n][k];
}
else
{
bins[n][k] = getBinomialCoefficient(n-1, k) + getBinomialCoefficient(n-1, k-1);
return bins[n][k];
}
}
}




--------------------------------------
1.

import java.util.Scanner;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 369 - Combinations
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=305
*
* @author Siegfried Ippisch
* @version 1.0
*
* Method : -
* Status : Accepted
* Runtime: 0.684
*/
public class Combinations {

public static void main(String[] args){
Scanner in = new Scanner(System.in);

long n = in.nextLong();
long m = in.nextLong();

while(n != 0 || m != 0){
System.out.printf("%d things taken %d at a time is %d exactly.\n",n,m,f(n,m));
n = in.nextLong();
m = in.nextLong();
}

in.close();
}

public static int f(long n, long m){
double res = 1;
for(long i=0; i<m; i++){
res = res * (n-i);
res = res / (i+1);
}
return (int)(res+0.5);
}
}




2.



package acm_369_combinations;

import java.util.Scanner;

/**
* FWP, Ausgew¦hlte Probleme aus dem ACM Programming Contest, SS10
* Problem: acm_369_combinations
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&page=show_problem&problem=305&Itemid=8
*
* @author Martin Lambeck
* @version 1.0, 18.08.2010
*
* Method : adhoc
* Status : Accepted
* Runtime: 0.780
*/


public class Main
{
static Scanner sc = new Scanner(System.in);

public static void main(String... args)
{
while (testcase())
;
}

public static boolean testcase()
{

int n = sc.nextInt();
int m = sc.nextInt();

if ((n == 0) && (m==0))
return false;

double b = 1.0;

double mi = 1.0;
int ni = n;

int mmax = Math.min(m, n-m);

for (int i = 0; i < mmax; i++)
{
double v = ni / mi;

b *= v;

ni--;
mi++;
}

System.out.printf("%d things taken %d at a time is %d exactly.%n",n, m, (int) (b+0.001));

return true;
}
}