[BaekJoon] 11401 이항 계수 3

오태호·2023년 5월 31일
0

백준 알고리즘

목록 보기
237/395
post-thumbnail

1.  문제 링크

https://www.acmicpc.net/problem/11401

2.  문제

3.  소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    final static long DIVISOR = 1_000_000_007;
    static long N, K;

    static void input() {
        Reader scanner = new Reader();

        N = scanner.nextLong();
        K = scanner.nextLong();
    }

    static void solution() {
        long numerator = factorial(N);
        long denominator = factorial(K) * factorial(N - K) % DIVISOR;

        System.out.println(numerator * pow(denominator, DIVISOR - 2) % DIVISOR);
    }

    public static long factorial(long num) {
        long multiply = 1L;

        while(num > 1) {
            multiply = (multiply * num) % DIVISOR;
            num--;
        }

        return multiply;
    }

    static long pow(long base, long exponent) {
        if(exponent == 1) return base % DIVISOR;

        long multiply = pow(base, exponent / 2);
        if(exponent % 2 == 1) return (multiply * multiply % DIVISOR) * base % DIVISOR;

        return multiply * multiply % DIVISOR;
    }

    public static void main(String[] args) {
        input();
        solution();
    }

    static class Reader {
        BufferedReader br;
        StringTokenizer st;

        public Reader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while(st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return st.nextToken();
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}
profile
자바, 웹 개발을 열심히 공부하고 있습니다!

0개의 댓글