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. android-------Android Studio使用MAT分析工具遇到的错误

    今天主要介绍一下我使用MAT工具分析文件时遇到的一个错误 Error opening heap dump 'a.hprof'. Check the error log for further deta ...

  2. Build Castles(构建城堡)

    Charlemagne, the King of Frankie, 英文描述 请参考图片中的说明. 中文描述 根据给出的数组确定能够盖多少城堡. 思路和点评 我不能确定我的思路是正确的,也欢迎大家参与 ...

  3. 小程序中this和that用法

    微信小程序中,在wx.request({});方法调用成功或者失败之后,有时候会需要获取页面初始化数据data的情况,这个时候,如果使用,this.data来获取,会出现获取不到的情况,调试页面也会报 ...

  4. Leetcode 129

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  5. GitHub学习四-查看版本库信息

    首先,只有在本地init以及关联到远程版本库的本地版本库,才能查看版本库信息 1.查看版本库名字 如果忘了名字,直接运行git remote 2.git remote show <name> ...

  6. Linux系统下tomcat的配置

    Linux系统下tomcat的配置 完成后可以输入命令查看日志文件: 最后进入网页测试下吧: 可以出来这个网页就好了

  7. python操作文件(增、删、改、查)

    内容 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode ...

  8. JSON 对象 与 字符串 互转

    $sui = [ 'xixixi' => 'suisuisui', 'hahaha' => 'longlonglong', ]; $data = json_encode($sui); pr ...

  9. MySQL数据库安装与配置鸡汤

    目录 一.概述 二.MySQL安装 三.安装成功验证 四.NavicatforMySQL下载及使用 一.概述 MySQL版本:5.7.17 下载地址:http://rj.baidu.com/soft/ ...

  10. js之DOM元素遍历

    对于元素间的空格,IE9之前的版本不会返回文本节点,而且他所有浏览器都会返回文本节点.这样就导致 使用childNodes和firstChild等属性时的行为不一致.从而有了Element Trave ...