SDAU练习三1020

来源:互联网 发布:有域名怎么免费建网站 编辑:程序博客网 时间:2024/04/28 01:31

Problem T

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 2
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.<br><br>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 101 2 4 2 1 12 51 4 2 10 0
 

Sample Output
84
 
题目大意:
现在有好多硬币,面值分别为   a1.a2....ai,给出每个硬币的数量,求可以组成多少个不同的价值。
思路:
这个是多重背包的问题。
关于多重背包求解当这件物品的数量*这件物品的体积  >=    背包容量   那么这件这就相当于完全背包,完全背包的定义:每件物品无限可用。
如果不够的话,那么就用  0  1  来解,把  1,2,3...k  件物品当成一件物品来看。
感想:
一个典型的问题,思路可以借鉴,水平有限,,代码写不好。,
AC代码:
  1. #include <cstdlib>  
  2. #include <cstring>  
  3. #include <cstdio>  
  4. #include <iostream>  
  5. using namespace std;  
  6. int val[900005];  
  7. int num[900005];  
  8. int dp[900005];  
  9. int v;  
  10. void ZeroOnePack(int cost,int weight)  
  11. {  
  12.     for(int i=v;i>=cost;i--)  
  13.        if(dp[i-cost]+weight>dp[i]) dp[i]=dp[i-cost]+weight;  
  14. }  
  15. void CompletePack(int cost,int weight)  
  16. {  
  17.     for(int i=cost;i<=v;i++)  
  18.         if(dp[i-cost]+weight>dp[i]) dp[i]=dp[i-cost]+weight;  
  19. }  
  20. void MultiplePack(int cost ,int weight,int amount)  
  21. {  
  22.     if(cost*amount>=v) CompletePack(cost,weight);  
  23.     else  
  24.     {  
  25.         for(int k=1;k<amount;)  
  26.         {  
  27.             ZeroOnePack(k*cost,k*weight);  
  28.             amount-=k;  
  29.             k<<=1;  
  30.         }  
  31.         ZeroOnePack(amount*cost,amount*weight);  
  32.     }  
  33. }  
  34. int main()  
  35. {  
  36.     //freopen("r.txt","r",stdin);  
  37.     int i,n,m,j;  
  38.   
  39.     while(~scanf("%d%d",&n,&v))  
  40.     {  
  41.         if(n==0&&v==0) break;  
  42.   
  43.         for(i=0;i<n;i++)  
  44.         {  
  45.             scanf("%d",&val[i]);  
  46.         }  
  47.         for(i=0;i<n;i++)  
  48.         {  
  49.             scanf("%d",&num[i]);  
  50.         }  
  51.   
  52.         memset(dp,0,sizeof(dp));  
  53.   
  54.         for(i=0;i<n;i++)  
  55.         {  
  56.             MultiplePack(val[i],val[i],num[i]);  
  57.         }  
  58.         int temp=0,cou=0;  
  59.         for(i=0;i<=v;i++)  
  60.         {  
  61.             if(temp!=dp[i])  
  62.             {  
  63.                 temp=dp[i];  
  64.                 cou++;  
  65.             }  
  66.         }  
  67.         cout<<cou<<endl;  
  68.     }  
  69.     return 0;  
  70. }  

0 0