链接:
https://www.acwing.com/problem/content/283/
题意:
给定N种硬币,其中第 i 种硬币的面值为Ai,共有Ci个。
从中选出若干个硬币,把面值相加,若结果为S,则称“面值S能被拼成”。
求1~M之间能被拼成的面值有多少个。
思路:
考虑多重背包,复杂度较高,因为是判断能否组成所需的硬币,可以从前往后,另开数组记录第i种硬币的使用数目.
代码:
#include <bits/stdc++.h>
using namespace std;
int a[110], c[110];
int Dp[100010], Cnt[100010];
int Is[100010];
int n, m;
int main()
{
while (~scanf("%d%d", &n, &m) && (n || m))
{
memset(Is, 0, sizeof(Is));
for (int i = 1;i <= n;i++)
scanf("%d", &a[i]);
for (int i = 1;i <= n;i++)
scanf("%d", &c[i]);
Is[0] = 1;
for (int i = 1;i <= n;i++)
{
for (int j = 0;j <= m;j++)
Cnt[j] = 0;
for (int j = a[i];j <= m;j++)
{
if (!Is[j] && Is[j-a[i]] && Cnt[j-a[i]] < c[i])
Is[j] = 1, Cnt[j] = Cnt[j-a[i]]+1;
}
}
int ans = 0;
for (int i = 1;i <= m;i++)
if (Is[i])
ans++;
printf("%d
", ans);
}
return 0;
}