Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6279    Accepted Submission(s): 2561

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
题解:英语不好,果然个个都赶脚是坑啊,唉,题意千万读懂了再做题,本题是多重背包的题目,A  表示硬币的价值, C  表示对应硬币的数量;典型的完全背包啊;
     1.首先是 n 表示 n 组数据,第一行输入价值 A, 第二行输入价值对应的数量 C ;用这些价值的硬币,组合出在 1 和 m ,之间的数(包括1,m);
     2.所以我们可以把,多重背包分成 01 和 完全背包 来解;如果遇见 A[i]*[i]>=m 按照完全背包,否则 01 背包;
AC代码一:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. int v[];
  5. int w[];
  6. int dp[];
  7. using namespace std;
  8. int main()
  9. {
  10. int n,m;
  11. while(scanf("%d%d",&n,&m)!=EOF)
  12. {
  13. if(n==&&m==)
  14. break;
  15. memset(v,,sizeof(v));
  16. memset(w,,sizeof(w));
  17. for(int i=;i<=;i++)
  18. dp[i]=-;
  19. dp[]=;
  20. for(int i=; i<=n; i++)
  21. scanf("%d",&v[i]);
  22. for(int i=; i<=n; i++)
  23. scanf("%d",&w[i]);
  24. for(int i=; i<=n; i++)
  25. {
  26. if(v[i]*w[i]>=m)//完全背包
  27. {
  28. for(int j=v[i]; j<=m; j++)
  29. {
  30. dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
  31. }
  32. }
  33. else
  34. {
  35. for(int k=; k<=w[i]; k=k*)
  36. {
  37. for(int j=m; j>=v[i]*k; j--)
  38. {
  39. dp[j]=max(dp[j],dp[j-v[i]*k]+v[i]*k);
  40. }
  41. w[i]-=k;
  42. }
  43. if(w[i]>)
  44. {
  45. for(int j=m; j>=v[i]*w[i]; j--)
  46. dp[j]=max(dp[j],dp[j-v[i]*w[i]]+v[i]*w[i]);
  47. }
  48. }
  49. }
  50. int count=;
  51. for(int i=; i<=m; i++)
  52. {
  53. if(dp[i]>=)
  54. count++;
  55. }
  56. printf("%d\n",count);
  57. }
  58. return ;
  59. }

AC代码二:

  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4.  
  5. int a[],c[],F[];
  6.  
  7. void inline ZeroOnePack(int ResVal,int ResVol,int BpCap)
  8. {
  9. for(int i=BpCap;i>=ResVol;--i)
  10. {
  11. F[i]=max(F[i],F[i-ResVol]+ResVal);
  12. }
  13. }
  14.  
  15. void inline CompletePack(int ResVal,int ResVol,int BpCap)
  16. {
  17. for(int i=ResVol;i<=BpCap;++i)
  18. {
  19. F[i]=max(F[i],F[i-ResVol]+ResVal);
  20. }
  21. }
  22.  
  23. void MultiplePack(int ResVal,int ResVol,int ResNum,int BpCap)
  24. {
  25. if(ResVol*ResNum>=BpCap)
  26. { CompletePack(ResVal,ResVol,BpCap); }
  27. for(int i=;(<<i)<=ResNum;++i)
  28. {
  29. ZeroOnePack((ResVal<<i),(ResVol<<i),BpCap);
  30. ResNum-=(<<i);
  31. }
  32. if(ResNum) { ZeroOnePack(ResVal*ResNum,ResVol*ResNum,BpCap); }
  33. }
  34.  
  35. int main()
  36. {
  37. int i,j,n,m;
  38. while(cin>>n>>m)
  39. {
  40. if(n+m==)
  41. break;
  42. memset(F,,sizeof(F));
  43. for(i=;i<n;i++)
  44. cin>>a[i];
  45. for(j=;j<n;j++)
  46. cin>>c[j];
  47. for(i=;i<n;i++)
  48. {
  49. MultiplePack(a[i],a[i],c[i],m) ;
  50. }
  51. int num=;
  52. for(i=;i<=m;i++)
  53. {
  54. if(F[i]==i)
  55. num++;
  56. }
  57. cout<<num<<endl;
  58. }
  59. return ;
  60. }

HDU 2844 Coin 多重背包的更多相关文章

  1. hdu 2844 Coins (多重背包+二进制优化)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...

  2. HDu -2844 Coins多重背包

    这道题是典型的多重背包的题目,也是最基础的多重背包的题目 题目大意:给定n和m, 其中n为有多少中钱币, m为背包的容量,让你求出在1 - m 之间有多少种价钱的组合,由于这道题价值和重量相等,所以就 ...

  3. HDU - 2844 Coins(多重背包+完全背包)

    题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...

  4. HDU 2844 Coins (多重背包计数 空间换时间)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  5. hdu 2844 coins(多重背包 二进制拆分法)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  6. hdu 2844 Coins 多重背包(模板) *

    Coins                                                                             Time Limit: 2000/1 ...

  7. hdu 1963 Investment 多重背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 //多重背包 #include <cstdio> #include <cstr ...

  8. 杭电1171 Big Event in HDU(母函数+多重背包解法)

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. hdu2844 &amp; poj1742 Coin ---多重背包--两种方法

    意甲冠军:你有N种硬币,每个价格值A[i],每个号码C[i],要求. 在不超过M如果是,我们用这些硬币,有多少种付款的情况下,.那是,:1,2,3,4,5,....,M这么多的情况下,,你可以用你的硬 ...

随机推荐

  1. 探寻C++最快的读取文件的方案 ——C++ IO优化

    在竞赛中,遇到大数据时,往往读文件成了程序运行速度的瓶颈,需要更快的读取方式.相信几乎所有的C++学习者都在cin机器缓慢的速度上栽过跟头,于是从此以后发誓不用cin读数据.还有人说Pascal的re ...

  2. Spark Streaming:大规模流式数据处理的新贵(转)

    原文链接:Spark Streaming:大规模流式数据处理的新贵 摘要:Spark Streaming是大规模流式数据处理的新贵,将流式计算分解成一系列短小的批处理作业.本文阐释了Spark Str ...

  3. 大端和小端(Big endian and Little endian)

    一.大端和小端的问题 对于整型.长整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节):而 Little endian 则相反,它 ...

  4. 实现微信小程序的3rd_session

    function 3rd_session($len) { $fp = @fopen('/dev/urandom','rb'); $result = ''; if ($fp !== FALSE) { $ ...

  5. Android倒计时案例展示

    1. Handler 与Message方法实现倒计时功能 关于Handler与Message消息机制的原理可查看:Android--Handler使用应运及消息机制处理原理分析 这个设计思路也是最经常 ...

  6. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  7. Shodan:黑客的物联网搜索引擎

    记得看过一个电影.里面的科学家开发了一个超级系统,能够实时监控全部可用摄像头.让逃犯无处遁形. Shodan这个新型的搜索引擎可能会让这个想法变成现实. 和Google这些传统互联网信息搜索引擎不同. ...

  8. Xcode5 打包 发布配置

    http://www.cnblogs.com/zhaoqingqing/p/3553750.html 主题 Unity导出Xcode项目,使用Xocde打包ipa并提交到AppStore 步骤 1.设 ...

  9. Android SDK 更新和下载慢怎么办?

    博客搬家:因为各种原因,我如今的博客将首发于blog.mojijs.com, 能够百度搜索 "姜哥的墨迹技术博客" , 或者 点击这里 本文地址 http://blog.mojij ...

  10. Field &#39;id&#39; doesn&#39;t have a default value问题解决方法

    Field 'id' doesn't have a default value问题解决方法 突然想温习温习对数据库的读写.于是就用mysql建了一张单独的表(见代码1),用Hibernate写了个应用 ...