File tree Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ // Authored by: 336699go
2+ // Co-authored by: -
3+ // Link: http://boj.kr/06a33314bada4e6cbb75b91eb3b4f498
4+
5+ import java .io .*;
6+ import java .util .*;
7+
8+ public class Main {
9+ public static void main (String [] args ) throws IOException {
10+ FastReader br = new FastReader ();
11+ int n = br .nextInt ();
12+ int k = br .nextInt ();
13+ int [] coins = new int [n ];
14+ int [] dp = new int [k + 1 ];
15+
16+ for (int i = 0 ; i < n ; i ++) {
17+ coins [i ] = br .nextInt ();
18+ }
19+
20+ dp [0 ] = 1 ;
21+
22+ for (int i = 0 ; i < n ; i ++) {
23+ for (int j = coins [i ]; j <= k ; j ++) {
24+ dp [j ] = dp [j ] + dp [j - coins [i ]];
25+ }
26+ }
27+
28+ System .out .println (dp [k ]);
29+ }
30+
31+ static class FastReader {
32+ BufferedReader br ;
33+ StringTokenizer st ;
34+
35+ public FastReader () {
36+ br = new BufferedReader (new InputStreamReader (System .in ));
37+ }
38+
39+ String next () {
40+ while (st == null || !st .hasMoreElements ()) {
41+ try {
42+ st = new StringTokenizer (br .readLine ());
43+ } catch (IOException e ) {
44+ e .printStackTrace ();
45+ }
46+ }
47+ return st .nextToken ();
48+ }
49+
50+ int nextInt () {
51+ return Integer .parseInt (next ());
52+ }
53+
54+ long nextLong () {
55+ return Long .parseLong (next ());
56+ }
57+
58+ double nextDouble () {
59+ return Double .parseDouble (next ());
60+ }
61+
62+ String nextLine () {
63+ String str = "" ;
64+ try {
65+ str = br .readLine ();
66+ } catch (IOException e ) {
67+ e .printStackTrace ();
68+ }
69+ return str ;
70+ }
71+ }
72+ }
73+
74+ /* Solution Description
75+ 배낭 문제와 유사하나 1+2, 2+1 같은 중복을 신경 써야 한다.
76+ 점화식: dp[j] = dp[j] + dp[j - coins[i]] = i번째 동전까지만 사용해서 가치의 합이 j원이 되는 경우의 수
77+ 메모리 제한이 빡빡해서 dp 배열을 2차원이 아닌 1차원 배열로 해야 한다.
78+ */
You can’t perform that action at this time.
0 commit comments