Problem Description:
Whuacmers use coins.They have coins of value A1,A2,A3…An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn’t know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3…An and C1,C2,C3…Cn corresponding to the number of Tony’s coins of value A1,A2,A3…An then calculate how many prices(form 1 to m) Tony can pay use these coins.
Input
The input contains several test cases. The first line of each test case contains two integers n(1 ≤ n ≤ 100),m(m ≤ 100000).The second line contains 2n integers, denoting A1,A2,A3…An,C1,C2,C3…Cn (1 ≤ Ai ≤ 100000,1 ≤ Ci ≤ 1000). The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8
4
题意:
给定硬币种类和数量,求能组成几种价格(<=m)
解题思路:
多重背包,优化(二进制/单调队列),母函数?(不会=3=)
代码:
#include<bits/stdc++.h>
#define rep(i,s,t) for(int i=s;i<=t;++i)
#define per(i,s,t) for(int i=s;i>=t;--i)
#define maxn 100003
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int dp[maxn],v[101],num[101],n,m;
void zero(int v){
per(k,m,v)dp[k]=max(dp[k],dp[k-v]+v); //0-1背包
}
void com(int v){
rep(k,v,m)dp[k]=max(dp[k],dp[k-v]+v); //完全背包
}
void mul(int v,int n){ //二进制优化 多重背包
if(v*n>=m) com(v);
else{
int k=1;
while(k<=n){
zero(k*v);
n-=k;
k<<=1;
}
zero(v*n);
}
}
int main(){
while(1){
n=read(),m=read();
if(n==0&&m==0) break;
memset(dp,0,sizeof(dp));
rep(i,0,n-1) v[i]=read();
rep(i,0,n-1) num[i]=read();
if(m<=0){
printf("0\n");
continue;
}
int cnt=0;
rep(i,0,n-1) mul(v[i],num[i]);
rep(i,1,m) if(dp[i]==i) ++cnt;
printf("%d\n",cnt);
}
return 0;
}