BOJ_11050 이항 계수 1
BOJ_11051 이항 계수 2
(링크 참조)
(링크 참조)
(링크 참조)
(링크 참조)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
long start = System.currentTimeMillis();
System.out.println(bc(n,k));
long end = System.currentTimeMillis();
System.out.println(end-start);
}
private static int bc(int n, int k) {
return factorial(n) / (factorial(n - k) * factorial(k));
}
private static int factorial(int value) {
int ans = 1;
if (value == 0) return 1;
for (int i = value; i >= 1; --i) {
ans *= i;
}
return ans;
}
}
import java.util.Scanner;
public class Main {
private static int[][] memo;
private static final int NUM = 10_007;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
memo = new int[1001][1001];
for (int i = 0; i <= n; i++) {
//(2')
memo[i][0] = 1;
memo[i][i] = 1;
}
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= i; j++) {
if (i == j || j == 0) {
//(2')
memo[i][j] = 1;
} else {
// (3)
memo[i][j] = memo[i-1][j] + memo[i-1][j-1];
}
memo[i][j] %= NUM;
}
}
System.out.println(memo[n][k]);
}
}
Dynamic Programming(DP)
를 사용했다.