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
#include<iostream>
#include<algorithm>
#include<string.h>
int dp[][100003];
int val[],num[];
using namespace std;
int main(){
int n,m;
while(cin>>n>>m&&!(n==&&m==)){
memset(dp,,sizeof(dp));
for(int i=;i<n;i++){
cin>>val[i];
}
for(int i=;i<n;i++){
cin>>num[i];
}
dp[][]=;
for(int i=;i<n;i++){
for(int j=;j<=m;j++){
for(int w=;w<=num[i]&&w*val[i]<=j;w++){
dp[i+][j]|=dp[i][j-w*val[i]];
}
}
}
int ans=count(dp[n]+,dp[n]++m,);
cout<<ans<<endl;
}
return ;
}
dp[i+1][j]表示前i种数字能否拼成j
一般用DP求取bool结果的话会有不少浪费,同样的复杂度可以获得很多信息

优化

dp[i+1][j]:用前i种数加和得到j时第i种数最多能剩几个

  1. dp[i][j] := 用前i种硬币凑成j时第i种硬币最多能剩余多少个(-1表示配不出来)
  2. 如果dp[i - 1][j] >= 0(前i-1个数可以凑出j,那么第i个数根本用不着)直接为C[i]
  3. dp[i][j] =  如果j < A[i]或者dp[i][j - a[i]] <=0 (面额太大或者在配更小的数的时候就用光了)-1
  4. 其他(将第i个数用掉一个) dp[i][j-a[i]] - 1
#include<iostream>
#include<algorithm>
#include<string.h>
int dp[][100003];
int val[],num[];
using namespace std;
int main(){
int n,m;
while(cin>>n>>m&&!(n==&&m==)){
memset(dp,-,sizeof(dp));
for(int i=;i<n;i++){
cin>>val[i];
}
for(int i=;i<n;i++){
cin>>num[i];
}
dp[][]=;
for(int i=;i<n;i++){
for(int j=;j<=m;j++){
if(dp[i][j]>=)
dp[i+][j]=num[i];
else if(j<val[i]||dp[i+][j-val[i]]<=){
dp[i+][j]=-;
}
else{
dp[i+][j]=dp[i+][j-val[i]]-;
}
}
}
int ans=;
for(int i=;i<=m;i++){
if(dp[n][i]!=-)
ans++;
}
cout<<ans<<endl;
}
return ;
}

数组重复利用

#include<iostream>
#include<algorithm>
#include<string.h>
int dp[];
int val[],num[];
using namespace std;
int main(){
int n,m;
while(cin>>n>>m&&!(n==&&m==)){
memset(dp,-,sizeof(dp));
for(int i=;i<n;i++){
cin>>val[i];
}
for(int i=;i<n;i++){
cin>>num[i];
}
dp[]=;
for(int i=;i<n;i++){
for(int j=;j<=m;j++){
if(dp[j]>=)
dp[j]=num[i];
else if(j<val[i]||dp[j-val[i]]<=){
dp[j]=-;
}
else{
dp[j]=dp[j-val[i]]-;
}
}
}
int ans=;
for(int i=;i<=m;i++){
if(dp[i]!=-)
ans++;
}
cout<<ans<<endl;
}
return ;
}

POJ1742--Coins(动态规划)的更多相关文章

  1. POJ1742 coins 动态规划之多重部分和问题

    原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表 ...

  2. POJ1742 Coins(男人八题之一)

    前言 大名鼎鼎的男人八题,终于见识了... 题面 http://poj.org/problem?id=1742 分析 § 1 多重背包 这很显然是一个完全背包问题,考虑转移方程: DP[i][j]表示 ...

  3. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  4. POJ1742:Coins(多重背包)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  5. poj1742 Coins【多重背包】【贪心】

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions:43969   Accepted: 14873 Descriptio ...

  6. poj1742 Coins(多重背包+单调队列优化)

    /* 这题卡常数.... 二进制优化或者单调队列会被卡 必须+上个特判才能过QAQ 单调队列维护之前的钱数有几个能拼出来的 循环的时候以钱数为步长 如果队列超过c[i]就说明队头的不能再用了 拿出来 ...

  7. POJ1742 Coins 背包

    题目大意:给出一些钱币的价值和对应的数目,求在一定价值限定下这些钱币能凑成的价值数. 本题用多重背包直接拆分或二进制拆分法都太慢.说起处理一组物品,完全背包可算是比较效率高的,但是本题中物体的数目是有 ...

  8. $POJ1742\ Coins$ 多重背包+贪心

    Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...

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

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

  10. 常规DP专题练习

    POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...

随机推荐

  1. Python使用filetype精确判断文件类型 (文件类型获取)

    filetype.py Small and dependency free Python package to infer file type and MIME type checking the m ...

  2. Linux locales

    一.简介   二.语法   三.实例 aptitude install locales dpkg-reconfigure locales ; vi /etc/default/locale more / ...

  3. ListView的自定义适配器及其优化(listView序号混乱问题的处理)

    ListView是最常使用的android组件之一,关于listView的优化问题刚刚了解了一些,在这里做出总结. PS:如果想让ListView中的item根据数据内容显示item的大小,需要在it ...

  4. 【Linux】zlib安装

    zlib简介 zlib是提供数据压缩用的函式库,由Jean-loup Gailly与Mark Adler所开发,初版0.9版在1995年5月1日发表.zlib使用DEFLATE算法,最初是为libpn ...

  5. mysql 5.7 linux环境下解压安装

    在CentOS linux环境安装mysql 一般rpm(或者yum),预编译和源码安装. 如果采用rpm或者yum安装,mysql的数据文件一般存放在/var/lib/mysql目录下,也就是会把d ...

  6. 必看的经典金融书籍推荐zz

    5. 现代企业财务管理,11th詹姆斯.C.范霍恩,经济科学出版社,2002 6. Financial market and corporate strategy,glinbratt, 四.金融计量 ...

  7. oracle merge into语法

    oracle的merge into语法,在这种情况下: 基于某些字段,存在就更新,不存在就插入 不需要先去判断一下记录是否存在,直接使用merge into oerge into 语法: MERGE ...

  8. 796. Rotate String

    class Solution { public: bool rotateString(string A, string B) { if(A.length()==B.length()&& ...

  9. C# 中的委托(Delegate)

    委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托(Delegate)特别用于实现事件和回调方法.所有的委托(Delegate)都派生自 System.D ...

  10. adb 相关命令 以及无法adb识别设备的解决方法

    [自己解决方法] 在-/.android/文件夹下面新建adb_usb.ini文件.里面写入设备的idVendor号(0x加上四位数字),然后输入 adb kill-server, 然后adb dev ...