forked from rustednickle/competitive-programming-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_change.cpp
More file actions
41 lines (35 loc) · 1.06 KB
/
Copy pathcoin_change.cpp
File metadata and controls
41 lines (35 loc) · 1.06 KB
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
#include <bits/stdc++.h>
using namespace std;
/*
* Input: types c.size() types of coins and value of coins for each time.
* Output: No. of ways to make changes for n value using given coins
*/
template <typename templateType>
templateType getWays(templateType n, vector<templateType> c){
templateType** a = (templateType**)malloc((n+1)*sizeof(templateType*));
for(int i=0;i<n+1;i++)a[i] = (templateType*)calloc(c.size(), sizeof(templateType));
/* Assumption: Coins of all types are in infintr quantity. */
for(int i=0;i<c.size();i++)a[0][i]=1;
for(templateType i=1;i<n+1;i++){
for(templateType j=0;j<c.size();j++){
templateType x = ((i-c[j])>=0)? a[i-c[j]][j]:0;
templateType y = (j>0)? a[i][j-1]:0;
a[i][j] = x+y;
}
}
return a[n][c.size()-1];
}
//test drivers function
int main() {
int n;
int m;
cin >> n >> m;
vector<long> c(m);
for(int c_i = 0; c_i < m; c_i++){
cin >> c[c_i];
}
// Print the number of ways of making change for 'n' units using coins having the values given by 'c'
long ways = getWays<long>(n, c);
cout<<ways<<endl;
return 0;
}