[ 문제 ]
자연수 과 정수 가 주어졌을 때 이항 계수 를 로 나눈 나머지를 구하는 프로그램을 작성하시오.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static int N, K;
static int P = 1000000007;
public static void main(String[] args) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
long num = factorial(N, P);
long denom = factorial(N - K, P) * factorial(K, P);
System.out.println((num * div(denom, P - 2, P)) % P);
}
// 팩토리얼 계산
private static long factorial(int n, int p){
if(n == 0) return 1;
if(n == 1) return 1 % p;
else return ((n % p) * pib(n - 1, p) % p) % p;
}
// 모듈러 연산 공식 이용
private static long div(long n, long m, long p){
if(m == 0) return 1 % p;
if(m == 1) return n % p;
else{
long tmp = div(n, m/2, p);
if(m % 2 == 1){
return (((tmp * tmp) % p) * (n % p)) % p;
} else{
return (tmp * tmp) % p;
}
}
}
}