Title Link: POJ-1742
Main topic
Existing n different coins, each with a face value of Vi, the number of Ni, ask how many of these coins can be used to make up [1,m] in the range of face value.
Problem analysis
Using an O (nm) DP (it is said to be a multi-class backpack?) ), enumerate each kind of coin, for each kind of coin I enumerate each face value J, if this face value J uses before the i-1 kind of coin already can gather out, directly skips, otherwise attempts to add a coin I, to see whether can gather out J. Need to meet (f[j-vi] = True) && (Usenum[j-vi] + 1 <= Ni), so you can. For each I, the usenum array is zeroed before enumeration J.
Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm>using namespace std;const int maxn = 5, MAXM = 100000 + 5;int N, M, Ans;int V[maxn], n UM[MAXN], Usenum[maxm];bool f[maxm];int main () {while (true) {scanf ("%d%d", &n, &m); if (n = = 0 && m = = 0) b reak;for (int i = 1; I <= n; ++i) scanf ("%d", &v[i]), for (int i = 1; I <= n; ++i) scanf ("%d", &num[i]); Ans = 0;for (int i = 1; I <= m; ++i) f[i] = false;f[0] = true;for (int i = 1; I <= n; ++i) {for (int j = 1; J <= M ++J) Usenum[j] = 0;for (int j = v[i]; j <= m; ++j) {if (F[j]) continue;if (F[j-v[i]] && Usenum[j-v[i]] + 1 <= Num[i]) {f[j] = true; USENUM[J] = Usenum[j-v[i]] + 1;}} for (int i = 1; I <= m; ++i) if (F[i]) ++ans;printf ("%d\n", Ans); }return 0;}
[POJ 1742] Coins "DP"