题意:有n(n <= 100)枚硬币面值为A1,A2,…,An,数量分别为C1,C2,…,Cn,求能组成多少种小于m的数额(m <= 100000)?
思路:不能暴力,对于Ai * Ci >= m的情况,直接完全背包。剩下的二进制拆分Ci,将Ci 拆分为 20+21+…+2k-1+(Ci-2k+1+1)。对于每一项进行零一背包即可。
http://acm.hdu.edu.cn/showproblem.php?pid=2844
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define cls(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5;
const int MAXE = 2e5;
int T,n,m;
int A[105],C[105];
int F[MAXN];
int a[12];
void ZEROPACK(int num) {
rrep(i,num,m) {
if(F[i-num] > 0) F[i]++;
}
}
void COMPPACK(int num) {
rep(i,num,m) {
if(F[i-num] > 0) F[i] ++;
}
}
void input() {
rep(i,1,n) {
scanf("%d",A+i);
}
rep(i,1,n) {
scanf("%d",C+i);
}
}
void solve() {
cls(F,0);
F[0] = 1;
rep(i,1,n) {
if(A[i] * C[i] >= m) {
COMPPACK(A[i]);
}
else {
int cnt = 1;
while(C[i] >= cnt) {
C[i] -= cnt;
ZEROPACK(A[i] * cnt);
cnt *= 2;
}
ZEROPACK(A[i] * C[i]);
}
}
int ans = 0;
rep(i,1,100000) {
if(F[i]) ans ++;
}
printf("%d\n",ans);
}
int main(void) {
int base = 1;
rep(i,1,10) {
a[i] = base;
base = base * 2;
}
while(scanf("%d %d",&n,&m),n+m) {
input();
solve();
}
return 0;
}