Test instructions: Given n kinds of coins, each value is a, the quantity is C, so that you can not be greater than the number of different values given V, that is, let you use these coins to make up the number of different prices, and the price is not greater than v.
Analysis: A look should know is a dynamic planning knapsack problem, but is the deformation, then we statistics not more than the different price of V, also easy to achieve,
For multiple backpacks we turned it into 01 backpacks and a complete backpack to solve.
The code is as follows:
#include <cstdio> #include <iostream> #include <cstring>using namespace std;typedef long Long ll;const int MAXN = 100000 + 10;int d[maxn];int C[MAXN], A[MAXN], v;void zeroonepack (int V, int val) {for (int i = V; I >= V; -i) D[i] = max (D[i], d[i-v]+val);} void Completepack (int v, int val) {for (int i = v; I <= v; ++i) d[i] = max (D[i], d[i-v]+val);} void Multiplepack (int v, int val, int num) {if (v <= num * v) {completepack (V, Val); return; } int k = 1; while (k <= num) {zeroonepack (v*k, val*k); num-= k; K <<= 1; } zeroonepack (Num*v, num*val);} int main () {int n; while (scanf ("%d%d", &n, &v)) {if (!n &&!) V) break; for (int i = 0; i < n; ++i) scanf ("%d", &a[i]); for (int i = 0; i < n; ++i) scanf ("%d", &c[i]); memset (d, 0, sizeof (d)); for (int i = 0; i < n; ++i) Multiplepack (A[i], a[i], c[i]); int cnt = 0;for (int i = 1; I <= V; ++i) if (d[i] = = i) ++cnt; printf ("%d\n", CNT); } return 0;}
HDU 2844 Coins (multiple knapsack problem DP)