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. Mycat水平拆分之十种分片规则

    水平切分分片实现   配置schema.xml  在同一个mysql数据库中,创建了三个数据库 testdb1,testdb2,testdb3.并在每个库中都创建了user表     <?xml ...

  2. 11月27日 招聘网第七部分,.gitignore的用法。

    回想Rails --Active Record Query Interface--Scopes的基本用法: 1.Passing in arguments.例子:scope  :name , -> ...

  3. 最长上升子序列 nlogn

    ; LL num[N]; LL dp[N]; LL go(LL l, LL r, LL k) { for (; r >= l; r--) if (dp[r] <= k) return r; ...

  4. IntelliJ IDEA 安装 Scala 插件

    本页面中对在 IntelliJ 中安装 Scala 插件的步骤和方法进行了描述. 需要在 IntelliJ  安装 Scala 插件,你首先需要在你的计算机中安装 IntelliJ .IntelliJ ...

  5. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

  6. java判断时间为上午,中午,下午,晚上,凌晨

    public static void main(String[] args) { Date date = new Date(); SimpleDateFormat df = new SimpleDat ...

  7. 使用org.apache.poi导出Excel表格

    public HSSFWorkbook MakeExcel(List<TransactionLogVO> logList) { // SimpleDateFormat sdf = new ...

  8. UI BOL 练习 get value set attr

    " " " " "**********************change list************************* "2 ...

  9. python使用MySQLdb模块连接MySQL

    1.安装驱动 目前有两个MySQL的驱动,我们可以选择其中一个进行安装: MySQL-python:是封装了MySQL C驱动的Python驱动:mysql-connector-python:是MyS ...

  10. Python内置模块之time、random、hashlib、OS、sys、UUID模块

    Python常用模块 1.time模块 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串: (1)时间戳(timestamp) :通常来说,时间 ...