-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcoinCHANGE.cpp
More file actions
48 lines (39 loc) · 827 Bytes
/
coinCHANGE.cpp
File metadata and controls
48 lines (39 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*************************************************
* The Legendry Coin Change Problem CodeChef - CTY2
* @author Amirul Islam (shiningflash) >> DP O(n)
*************************************************/
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e3;
int coin[mx], dp[mx];
int coin_change_way(int unit, int type) {
dp[0] = 1;
for (int i = 0; i < type; i++) {
for (int j = coin[i]; j <= unit; j++) {
dp[j] += dp[j - coin[i]];
}
}
return dp[unit];
}
int main() {
// freopen("in", "r", stdin);
int unit, type;
cin >> unit >> type;
for (int i = 0; i < type; i++) {
cin >> coin[i];
}
cout << coin_change_way(unit, type) << endl;
return 0;
}
/*
Input:
4 3
1 2 3
Output:
4
Explanation
{1, 1, 1, 1}
{1, 1, 2}
{2, 2}
{1, 3}
*/