-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCoin Combinations I.cpp
70 lines (50 loc) · 1.84 KB
/
Coin Combinations I.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
59
60
61
62
63
64
65
66
67
68
69
70
/*
Solution by Rahul Surana
***********************************************************
Consider a money system consisting of n coins. Each coin has a positive integer value.
Your task is to produce a sum of money x using the available coins in such a way that the number of coins is minimal.
For example, if the coins are {1,5,7} and the desired sum is 11, an optimal solution is 5+5+1 which requires 3 coins.
Input
The first input line has two integers n and x: the number of coins and the desired sum of money.
The second line has n distinct integers c1,c2,…,cn: the value of each coin.
Output
Print one integer: the minimum number of coins. If it is not possible to produce the desired sum, print −1.
***********************************************************
*/
#include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
int MOD=1e9+7;
int main() {
fast_io;
int n,x;
cin >> n >> x;
vector<int> ar(n);
for(auto& v:ar) {
cin >> v;
}
vector<int> dp(x+1,0);
dp[0] = 1;
// sort(ar.begin(),ar.end(),greater<ll>());
for(int i = 1; i < x+1; i++){
for(int j = 0; j < ar.size(); j++){
if( i >= ar[j]){
dp[i] += dp[i-ar[j]];
if(dp[i] >= MOD) dp[i] -= MOD;
}
}
}
cout << dp[x] <<"\n";
}