https://www.acmicpc.net/problem/2293
: 주어진 동전으로 K 를 만들 수 있는 경우의 수
경우의 수가 2의 31승이므로, 동적 프로그래밍(DP) 사용
import java.io.*;
import java.util.*;
public class Main {
static int N,K;
static int[] arr;
static int[] coinCase;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
arr = new int[N+1];
coinCase = new int[K+1];
coinCase[0] = 1;
for(int i=0;i<N;i++){
arr[i] = Integer.parseInt(br.readLine());
for(int j=arr[i];j<=K;j++){
coinCase[j] += coinCase[j-arr[i]];
}
}
System.out.println(coinCase[K]);
}
}
CoinCase 값
어떤 경우의 수로 이루어져 있는가?
이렇게 동전1의 경우의 수 + 동전 1,2의 경우의 수...+ 동전N개의 경우의 수 로 CoinCase 값을 얻을 수 있으며,
CoinCase[j] = CoinCase[j] + CoinCase[j-arr[i]] 의 점화식을 얻게 됨.