-
Notifications
You must be signed in to change notification settings - Fork 4
/
coin_change_totalways.cpp
62 lines (48 loc) · 1.1 KB
/
coin_change_totalways.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#define int long long int
#define double long double
#define PI acos(-1)
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
vi arr;
int len;
int dp[255][500];
int coin(int idx, int& store) {// store is ref variable for time optimisation
if (store < 0)
return 0;
if (idx == len) {
if (store == 0)
return 1;
else {
return 0;
}
}
if (dp[idx][store] != -1)
return dp[idx][store];
store -= arr[idx];// ref variable modified
int A = coin(idx, store);
store += arr[idx];// ref variable restored
int B = coin(idx + 1, store);
return dp[idx][store] = A + B;
}
void solve() {
int n, m;
cin >> n >> m;
arr.assign(m, 0);
for (int i = 0; i < m; i++) {
cin >> arr[i];
}
len = m;
int store = n;
memset(dp, -1, sizeof(dp));
int ways = coin(0, store);// traverse from total change to 0 change
cout << ways << endl;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("error.txt", "w", stderr);
#endif
crap;
solve();
return 0;
}