-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum_denom.cpp
More file actions
56 lines (53 loc) · 1.05 KB
/
num_denom.cpp
File metadata and controls
56 lines (53 loc) · 1.05 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
using namespace std;
int num_change2(int n,int *d,int k,int **dp){
if (n == 0){
return 1;
}
if(n < 0){
return 0;
}
if(k == 0){
return 0;
}
if(dp[n][k] >= 0){
return dp[n][k];
}
int option1 = num_change2(n-d[0],d,k,dp);
int option2 = num_change2(n,d + 1,k-1,dp);
dp[n][k] = option1 + option2;
return dp[n][k];
}
// Recursive solution
int num_change(int n,int *d,int k){
if (n == 0){
return 1;
}
if(n < 0){
return 0;
}
if(k == 0){
return 0;
}
int option1 = num_change(n-d[0],d,k);
int option2 = num_change(n,d + 1,k-1);
return option1 + option2;
}
int main(){
int n,k; // 'n' denotes value for which we want change and 'k' denotes the number of denomination
cin>>n>>k;
// int d[] = {1,2,3};
// int d[] = {2,3,5};
// int d[] = {1,7,10};
int **dp = new int*[n+1];
for(int i = 0;i<=n;i++){
dp[i] = new int[n+1];
}
for(int i=0;i<=n;i++){
for(int j = 0;j<=n;j++){
dp[i][j] = -1;
}
}
cout<<num_change(n,d,k)<<endl;
cout<<num_change2(n,d,k,dp)<<endl;
}