Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box 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
 
题目的意思:
  第一行输入,n,m分别表示n种硬币,m表示总钱数。
  第二行输入n个硬币的价值,和n个硬币的数量。
  输出这些硬币能表示的所有在m之内的硬币种数。

思路:此问题可以转换为01背包和完全背包问题求解,而01背包又可以进行二进制的优化
例:13可由1 2 4 6这四个数字可以组成从1到13所有的数字,并且还不会重复使用,1--->1; 2--->2; 3--->1,2; 4--->4; 5--->1,4; 6--->2,4; 7--->1,6; 8--->2,6; 9--->1,2,6; 10--->4,6; 11--->1,4,6; 12--->2,4,6; 13--->1,2,4,6;
 拆分方法:
{
  num;数字
  k=1;
  val;价值
  while(k<=num)
  {
    01beibao(k*val);
    num-=k;
    k=k*2;
  }
  01beibao(num);
}
代码:http://blog.csdn.net/vvmame/article/details/76697198
自己的代码:

#include<stdio.h>
#include<string.h>
struct
{
  int p;
  int nb;
}mp[150];
int dp[100050];//记录该点是否可以有硬币的组成而得到
int lg[100050];//记录每个状态硬币使用次数
int main()
{
  int n,m;
  int i,j,k,ans;
  while(scanf("%d%d",&n,&m)!=EOF&&(m!=0||n!=0))
  {
    ans=0;
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(i=1;i<=n;i++)
    scanf("%d",&mp[i].p);
    for(i=1;i<=n;i++)
    scanf("%d",&mp[i].nb);
    for(i=1;i<=n;i++)
    {
      memset(lg,0,sizeof(lg));
      for(j=mp[i].p;j<=m;j++)
      {
        if(!dp[j]&&dp[j-mp[i].p]&&lg[j-mp[i].p]<mp[i].nb)
        {
          dp[j]=1;
          ans++;
          lg[j]=lg[j-mp[i].p]+1;
        }
      }
    }
    printf("%d\n",ans);
  }
return 0;
}

poj1742(多重背包分解+01背包二进制优化)的更多相关文章

  1. 背包问题(01背包,完全背包,多重背包(朴素算法&&二进制优化))

    写在前面:我是一只蒟蒻~~~ 今天我们要讲讲动态规划中~~最最最最最~~~~简单~~的背包问题 1. 首先,我们先介绍一下  01背包 大家先看一下这道01背包的问题  题目  有m件物品和一个容量为 ...

  2. CodeCraft-19 and Codeforces Round #537 (Div. 2) D 多重排列 + 反向01背包 + 离线处理

    https://codeforces.com/contest/1111/problem/D 多重排列 + 反向01背包 题意: 给你一个字符串(n<=1e5,n为偶数),有q个询问,每次询问两个 ...

  3. POJ - 1276 二进制优化多重背包为01背包

    题意:直接说数据,735是目标值,然后3是后面有三种钱币,四张125的,六张五块的和三张350的. 思路:能够轻易的看出这是一个多重背包问题,735是背包的容量,那些钱币是物品,而且有一定的数量,是多 ...

  4. hdu1059 多重背包(转换为01背包二进制优化)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1059 之前写过一个多重背包二进制优化的博客,不懂请参考:http://www.cnblog ...

  5. HDU2191--多重背包(二进制分解+01背包)

    悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  6. HDU 2191(多重背包转换为01背包来做)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 Time Limit: 1000/1000 ...

  7. POJ3211 Washing Clothes[DP 分解 01背包可行性]

    Washing Clothes Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9707   Accepted: 3114 ...

  8. 5. 多重背包问题 II 【用二进制优化】

    多重背包问题 II 描述 有 NN 种物品和一个容量是 VV 的背包. 第 ii 种物品最多有 sisi 件,每件体积是 vivi,价值是 wiwi. 求解将哪些物品装入背包,可使物品体积总和不超过背 ...

  9. 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

随机推荐

  1. (Gorails视频)使用推广链接(params[:ref]),增加注册用户!

    用一个链接进行用户的注册推广: 我的git:   https://github.com/chentianwei411/embeddable_comments 用途:比如推广,拉朋友注册,给推广码,用这 ...

  2. 微信小程序分享

    点击链接查看详情:(转发的路径的必须写正确) https://mp.weixin.qq.com/debug/wxadoc/dev/api/share.html

  3. php 商场收银收费系统,使用的策略模式

    <?php//策略模式就是你有很多的方法,选择一种适合自己的,// 单例模式就是只有一个实例对象,不需要每个文件都要加载,比如连接数据库,// 工厂模式就是 //策略模式 优惠系统.工资计算系统 ...

  4. World Tour CodeForces - 667D (bfs最短路)

    大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大

  5. Python安装第三方库,报错超时: Read timed out.

    1.安装beautifulsoup4 >pip install beautifulsoup4 报错超时: Read timed out. 2.解决办法:pip --default-timeout ...

  6. 【JS】【2】ajax传的参数为数组时,后台接收为null的处理

    前言: 1,参考博客:解决JavaScript中使用$.ajax方式提交数组参数 - Just_Do - 博客园(http://www.cnblogs.com/caoyc/p/5710702.html ...

  7. ubuntu server 启用mysql日志

    1.要启动mysql日志,你就要找到mysql 核心的文件my.cnf  (路径:/etc/mysql) 在命令窗口输入:cd /etc/mysql 在命令窗口输入:ls 你就可以看到my.cnf文件 ...

  8. Leetcode 1005. K 次取反后最大化的数组和

    1005. K 次取反后最大化的数组和  显示英文描述 我的提交返回竞赛   用户通过次数377 用户尝试次数413 通过次数385 提交次数986 题目难度Easy 给定一个整数数组 A,我们只能用 ...

  9. 【LeetCode】N数和

    1. 3Sum 给定一个无序数组(可能存在重复元素),是否存在三个数之和为0,输出所有不重复的三元组. e.g. 给定数组 [-1, 0, 1, 2, -1, -4], 结果集为:[ [-1, 0, ...

  10. python requests 的cookie 操作

    结论: 1.requests模块的请求和响应分别有cookie对象. 可以通过此对象设置和获取cookie. 2.通过在requests.get,requests.post等方法请求中传入cookie ...