-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoins.cpp
27 lines (26 loc) · 852 Bytes
/
coins.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
#include <iostream>
using namespace std;
int main() {
int N, M;//amount and nr of coin types
long long dp[50][251];
long long C[50];
cin >> N >> M;
//input values
for (int i = 0; i < M; i++) cin >> C[i];
//ways to change amounts with first coin type
for (int j = 1; j <= N; j++)
if (j % C[0] == 0) dp[0][j] = 1;
//ways to get 0 with i coin types
for (int i = 0; i < M; i++) dp[i][0] = 1;
//ways to change amounts with i coin types
for (int i = 1; i < M; i++) {
for (int j = 1; j < C[i]; j++) dp[i][j] = dp[i-1][j];
for (int j = C[i]; j <= N; j++)
//all the ways to change j with i-1 coin types
//and all the ways to change j minus value of the ith coin type
dp[i][j] = dp[i-1][j] + dp[i][j-C[i]];
}
//ways to change N with M coin types
cout << dp[M-1][N] << endl;
return 0;
}