Description
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
Output
Sample Input
3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0
Sample Output
8 4
Source
K爷的模板
- struct Queue//V-背包最大容量 c[i]第i种物品数量 v[i]第i种物品价值 w[i]第i种物品重量 注意定义全局变量
- {
- int num,value;
- }que[250005];
- int head,tail;
- void enqueue (int x,int y)
- {
- while (head<=tail && que[tail].value<y) tail--;
- que[++tail].num=x;que[tail].value=y;
- }
- void multipack()
- {
- int i,j,d;
- memset(dp,0,sizeof(dp));
- for (i=1 ; i<=n ; ++i)
- {
- if (c[i] > V/w[i]) c[i]=V/w[i];
- for (d=0 ; d<w[i] ; ++d)
- {
- head=1;tail=0;
- for (j=0 ; j<=(V-d)/w[i] ; ++j)
- {
- enqueue(j , dp[j*w[i]+d]-j*v[i]);
- while (que[head].num<j-c[i] && head<=tail) head++;
- dp[j*w[i]+d]=que[head].value+j*v[i];
- }
- }
- }
- }
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxV=100005;
const int maxn=105;
int c[maxn],w[maxn];
int n,V;
bool dp[maxV];
void ZeroOnePack(int cost){
for(int i=V;i>=cost;i--)dp[i]|=dp[i-cost];
}
void CompletePack(int cost){
for(int i=cost;i<=V;i++)dp[i]|=dp[i-cost];
}
void MultiplePack()
{
for(int i=1;i<=n;i++)
{
if(w[i]*c[i]>=V)CompletePack(w[i]);
else {
int k=1;
while(k<c[i]){
ZeroOnePack(k*w[i]);
c[i]-=k;
k<<=1;
}
ZeroOnePack(c[i]*w[i]);
}
}
}
int main(){
while(scanf("%d%d",&n,&V)&&n&&V)
{
for(int i=1;i<=n;i++)
cin>>w[i];
for(int i=1;i<=n;i++)
cin>>c[i];
for(int i=0;i<=V;i++)
dp[i]=0;
dp[0]=1;
MultiplePack();
int ans=0;
for(int i=1;i<=V;i++)if(dp[i])ans++;
printf("%d\n",ans);
}
return 0;
}