Skip to content

Commit 45ca485

Browse files
committed
[Gold IV] Title: 동전 1, Time: 288 ms, Memory: 18748 KB -BaekjoonHub
1 parent b83276d commit 45ca485

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# [Gold IV] 동전 1 - 2293
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2293)
4+
5+
### 성능 요약
6+
7+
메모리: 18748 KB, 시간: 288 ms
8+
9+
### 분류
10+
11+
다이나믹 프로그래밍
12+
13+
### 제출 일자
14+
15+
2025년 2월 24일 16:33:22
16+
17+
### 문제 설명
18+
19+
<p>n가지 종류의 동전이 있다. 각각의 동전이 나타내는 가치는 다르다. 이 동전을 적당히 사용해서, 그 가치의 합이 k원이 되도록 하고 싶다. 그 경우의 수를 구하시오. 각각의 동전은 몇 개라도 사용할 수 있다.</p>
20+
21+
<p>사용한 동전의 구성이 같은데, 순서만 다른 것은 같은 경우이다.</p>
22+
23+
### 입력
24+
25+
<p>첫째 줄에 n, k가 주어진다. (1 ≤ n ≤ 100, 1 ≤ k ≤ 10,000) 다음 n개의 줄에는 각각의 동전의 가치가 주어진다. 동전의 가치는 100,000보다 작거나 같은 자연수이다.</p>
26+
27+
### 출력
28+
29+
<p>첫째 줄에 경우의 수를 출력한다. 경우의 수는 2<sup>31</sup>보다 작다.</p>
30+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
import java.io.*;
3+
public class Main {
4+
static int n, k;
5+
static int[] coins;
6+
static int[][] dp;
7+
static StringTokenizer st;
8+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
public static void main(String[] args) throws Exception{
10+
pre_setting();
11+
System.out.println(recur(0, 0));
12+
}
13+
14+
15+
static int recur(int now, int sum){
16+
if(k == sum) return 1;
17+
if(k < sum || n <= now) return 0;
18+
if(dp[now][sum] != -1) return dp[now][sum];
19+
20+
int rot = 0;
21+
for(int i = 0; sum + (i * coins[now]) <= k; i++){
22+
rot += recur(now + 1, sum + coins[now] * i);
23+
}
24+
return dp[now][sum] = rot;
25+
}
26+
27+
static void pre_setting() throws Exception{
28+
st = new StringTokenizer(br.readLine());
29+
30+
n = Integer.parseInt(st.nextToken());
31+
k = Integer.parseInt(st.nextToken());
32+
dp = new int[n][k + 1];
33+
coins = new int[n];
34+
35+
for(int i = 0; i < n; i++) {
36+
coins[i] = Integer.parseInt(br.readLine());
37+
Arrays.fill(dp[i], -1);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)