-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoin_Combinations_II.cpp
More file actions
38 lines (32 loc) · 1012 Bytes
/
Copy pathCoin_Combinations_II.cpp
File metadata and controls
38 lines (32 loc) · 1012 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define forn(i, k, e) for (int i = k; i < e; i++)
/*
count number of ways to get sum = x by using n coins
(one coin can be used more than once)
count(x) = 1 if x == coin value
count(x) = 0 if x < coin value
for each coin in coins:
count(x) = sum(count(x-coin)) for every x from 0 to sum
*/
const int MOD = 1e9 + 7;
int _count[1000001];
int32_t main() {
memset(_count, 0, sizeof(_count));
int n, sum;
cin >> n >> sum;
vector<int> coins(n);
forn(i, 0, n) cin >> coins[i];
sort(coins.begin(), coins.end());
_count[0] = 1; // set _count[0] = 1 to add 1 when coin value == sum
for (int coin : coins) {
// iterate through coins first to preserve order
// _count[x] calculates the `contribution of current coin` to get to sum x
forn(x, 0, sum + 1) {
if (coin > x)
continue;
_count[x] = (_count[x] + _count[x - coin]) % MOD;
}
}
cout << _count[sum];
}