题目描述说: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.
就是说花多少钱买多少钱东西,没有找零,dp[i]=i,i==dp[i](1<=i<=m);题目多重背包模型
代码
#include<stdio.h>
#include<string.h>
int dp[100010];
int n,m;
int Max(int a,int b)
{
return a>b?a:b;
}
void Completepack(int cost,int maxm)
{
int i;
for(i=cost;i<=maxm;i++)
dp[i]=Max(dp[i-cost]+cost,dp[i]);
}
void Zeronepack(int cost,int maxm)
{
int i;
for(i=maxm;i>=cost;i--)
dp[i]=Max(dp[i-cost]+cost,dp[i]);
}
void Multipackage(int a[],int c[],int n,int m)
{
int i,k,sum;
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
{
if(a[i]*c[i]>m)
Completepack(a[i],m);
else
{
k=1;
while(k<c[i])
{
Zeronepack(k*a[i],m);
c[i]-=k;
k*=2;
}
Zeronepack(c[i]*a[i],m);
}
}
sum=0;
for(i=1;i<=m;i++)
if(dp[i]==i) sum++;
printf("%d\n",sum);
}
int main()
{
int a[110],c[110];
int i;
while(scanf("%d%d",&n,&m)!=EOF && n!=0 && m!=0)
{
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&c[i]);
Multipackage(a,c,n,m);
}
}