[ 백준 ] 11050 이항 계수 1

codesver·2022년 12월 3일
0

Baekjoon

목록 보기
46/72
post-thumbnail

Solution

nCk_nC_k 를 factorial 공식으로 변환하면 다음과 같다.

n!(nk)!k!\frac{n!}{(n-k)!k!}

이를 다시 다음과 같이 바꿀 수 있다.

n(n1)(n2)...(nk+1)k(k1)(k2)...1\frac{n(n-1)(n-2)...(n-k+1)}{k(k-1)(k-2)...1}

분자와 분모 모두 k개이며 1부터 k까지 반복하면서 분모 분자를 각각 계산하여 해결 가능하다.

private static void solution() throws IOException {
    StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
    int n = Integer.parseInt(tokenizer.nextToken());
    int k = Integer.parseInt(tokenizer.nextToken());
    int up = 1, down = 1;
    for (int i = 1; i <= k; i++) {
        up *= n - i + 1;
        down *= i;
    }
    result.append(up / down);
}
profile
Hello, Devs!

0개의 댓글