
자연수 N과 정수 K가 주어졌을 때 이항 계수 를 1,000,000,007로 나눈 나머지를 구하는 프로그램을 작성하시오.
첫째 줄에 N과 K가 주어진다. (1 ≤ N ≤ 4000000, 0 ≤ K ≤ N)
를 1,000,000,007로 나눈 나머지를 출력한다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ11051 {
static final long P = 1_000_000_007;
private static void solution() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
long N = Long.parseLong(st.nextToken());
long K = Long.parseLong(st.nextToken());
long top = factorial(N);
long bottom = factorial(K) * factorial(N - K) % P;
System.out.println(top * compute(bottom, P - 2) % P);
}
private static long factorial(long N) {
long num = 1L;
while (N > 1) {
num = (num * N) % P;
N--;
}
return num;
}
static long compute(long a, long b) {
if (b == 1) {
return a % P;
}
long tmp = compute(a, b / 2);
if (b % 2 == 1) {
return (tmp*tmp % P) * a % P;
}
return tmp * tmp % P;
}
public static void main(String[] args) throws IOException {
BOJ11051.solution();
}
}

백준 이항 계수 시리즈에 대한 전체적인 내용은 여기를 참고하면 좋다.