https://www.acmicpc.net/problem/11051
nCr = n-1Cr + n-1Cr-1
틀렸습니다
가 뜬다.import java.io.*;
import java.util.*;
public class Main {
static int[][] cal = new int[1001][1001];
public static int comb(int a, int b) {
if (cal[a][b] != 0)
return cal[a][b];
if (b == 0 || a == b) {
return cal[a][b] = 1;
}
return cal[a][b] = comb(a-1, b) + comb(a-1, b-1);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
System.out.println(comb(a, b) % 10007);
}
}
1 ≤ N ≤ 1000
이므로 너무 숫자가 커져 overflow가 발생하여 잘못된 값을 발생시키기 때문이다.import java.io.*;
import java.util.*;
public class Main {
static int[][] cal = new int[1001][1001];
public static int comb(int a, int b) {
if (cal[a][b] != 0)
return cal[a][b];
if (b == 0 || a == b) {
return cal[a][b] = 1;
}
return cal[a][b] = (comb(a-1, b)%10007 + comb(a-1, b-1)%10007)%10007;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
System.out.println(comb(a, b));
}
}