Trang

Chủ Nhật, 2 tháng 1, 2011

1138. Binomial Showdown

import java.io.*;
import java.math.BigInteger;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp1138.html
     * @author hunglee
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        
        long n, k;
        do {
            in.nextToken();
            n = (long) in.nval;
            in.nextToken();
            k = (long) in.nval;
            if (n == 0 && k == 0) break;
            
            long i;
            BigInteger res = new BigInteger("1");
            BigInteger res2 = new BigInteger("1");
            if (k > n - k) k = n - k;
            for (i = n - k + 1; i <= n; ++i)
                res = res.multiply(BigInteger.valueOf(i));
            for (i = 2; i <= k; ++i)
                res2 = res2.multiply(BigInteger.valueOf(i));
            out.println(res.divide(res2));
        } while (true);
        out.flush();
    }

}

Không có nhận xét nào:

Đăng nhận xét