-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinChange2.cpp
58 lines (43 loc) · 1.06 KB
/
CoinChange2.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
//Bismillahir Rahman-ir Rahim
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << '>' << #x << " : " << x << endl;
#define all(c) c.begin(), c.end()
#define F first
#define S second
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef unsigned long long ull;
typedef long long ll;
/*
DP variant 2
We have to find the number of ways to choose coins to change n
if coins are {2, 3}
then we can make 5 in 2 ways: {2+3}, {3+2};
note: here we can use coins any number of times and order DOES matter
*/
vector <int> coins;
vector <int> ways(10000);
void calculateWays(int n){
ways[0] = 1;
for(int i = 1; i <= n; i++){
for(int j = 0; j < coins.size(); j++){
if(i >= coins[j]){
ways[i] += ways[i - coins[j]];
}
}
}
}
int main(){
int m;
cin >> m;
for(int i = 0; i < m; ++i){
int x;
cin >> x;
coins.push_back(x);
}
int n;
cin >> n;
calculateWays(n);
cout << ways[n] << endl;
return 0;
}