Test instructions: There are n denominations of coins a[i], each coin has c[i], asked to be composed of not less than the number of M (1~m).
Multi-Backpack Template: by backpack nine speaking.
#include <cstdio> #include <stdlib.h> #include <string.h> #include <string> #include <map > #include <cmath> #include <iostream> #include <queue> #include <stack> #include < Algorithm> #include <set>using namespace std; #define INF 1e8#define EPS 1e-8#define LL long long#define MAXN 10000 1#define mol 1000000007//procedure multiplepack (cost,weight,amount)//If cost*amount>=v//CompletePack (cost , weight)//return//integer k=1//while k<amount//zeroonepack (k*cost,k*weight)//Amoun t=amount-k//k=k*2//zeroonepack (amount*cost,amount*weight) int n,m,a[110],c[110];int dp[maxn];void COMPLETEPAC K (int cost,int weight)//Total backpack {for (int v=cost;v<=m;v++) Dp[v]=max (dp[v],dp[v-cost]+weight);} void zeroonepack (int cost,int weight)//01 backpack {for (int v=m;v>=cost;v--) Dp[v]=max (dp[v],dp[v-cost]+weight);} void Multiplepack (int cost,int weight,int amount) {if (cost*amount>=m) completepack (cost,weight); else {int K=1;while (k<amount)//binary split {zeroonepack (k*cost,k*weight); amount=amount-k; k=k*2;} Zeroonepack (Amount*cost,amount*weight);}} int main () {while (scanf ("%d%d", &n,&m)) {if (n==0&&m==0) break;for (int i=1;i<=n;i++) scanf ("%d", &a[i]), for (int i=1;i<=n;i++) scanf ("%d", &c[i]), and for (int i=1;i<=m;i++) dp[i]=0;for (int i=1;i<=n;i++) Multiplepack (A[i],a[i],c[i]), int ans=0;for (int i=1;i<=m;i++) if (dp[i]==i) ans++;p rintf ("%d\n", ans);} return 0;} /*3 101 2 4 2 1 12 51 4 2 1*/
HDU 2844 Coins (multiple backpack)