hdu 2844 Coins (多重背包)

时间:2023-03-08 22:27:38

题意是给你几个数,再给你这几个数的可以用的个数,然后随机找几个数来累加,

让我算可以累加得到的数的种数!

解题思路:先将背包初始化为-1,再用多重背包计算,最后检索,若bb[i]==i,则说明i这个数是可以得到的!一个循环计算可以达到的数的个数,最后输出就好了!

#include<stdio.h>

#define max(a,b) a>b?a:b

int bb[500000];

int vv;

void shun(int cost,int weight)

{


int i;


for(i=cost;i<=vv;i++)


bb[i]=max(bb[i],bb[i-cost]+weight);

}

void ni(int cost,int weight)

{


int i;


for(i=vv;i>=cost;i--)


bb[i]=max(bb[i],bb[i-cost]+weight);

}

int main()

{


int n,i,k,v[5000],amount[5000],ans;


while(scanf("%d%d",&n,&vv),n+vv)


{


for(i=1;i<=vv;i++)


bb[i]=-1;


for(i=0;i<n;i++)


scanf("%d",&v[i]);


for(i=0;i<n;i++)


scanf("%d",&amount[i]);


for(i=0;i<n;i++)


{


if(amount[i]*v[i]>=vv)


shun(v[i],v[i]);


else


{


k=1;


while(k<amount[i])


{


ni(k*v[i],k*v[i]);


amount[i]-=k;


k*=2;


}


ni(amount[i]*v[i],amount[i]*v[i]);


}


}


ans=0;


for(i=1;i<=vv;i++)


if(bb[i]==i)


ans++;


printf("%d\n",ans);


}


return 0;

}

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844