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
| #include <iostream>
#include <cstring>
using namespace std;
int dp[10001];
int coins[20];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int T;
cin >> T;
while (T--)
{
// input & init
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> coins[i];
}
int m;
cin >> m;
memset(dp, 0, (m + 1) * sizeof(int));
dp[0] = 1;
// dynamic programming
for (int i = 0; i < n; i++)
{
for (int j = coins[i]; j <= m; j++)
{
dp[j] += dp[j - coins[i]];
}
}
// output
cout << dp[m] << "\n";
}
return 0;
}
|
Leave a comment