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 创建一次性,快速的小型web服务

  2. CSS学习总结1:CSS样式

    1.CSS背景 属性 background-color:为元素设置背景色,值可以是任何合法的颜色值.实例:p {background-color: gray;} background-image:为元 ...

  3. SNP命名

    SNP命名 [2016-11-24]       奶茶妹妹是谁,京东老板娘,咦?章泽天!没错! 国民老公是谁?万达少东家,王健林儿子,王思聪!恭喜你又答对了! 函数是谁?这不是数学上的名词吗?不对,是 ...

  4. UIDataPicker 时间选择器

    自用时间选择器 @interface ViewController () { UILabel *cityLabel; UIDatePicker *datePicker; } //@property(n ...

  5. robot framework测试驱动无法定位页面元素

    robot framework错误提示: [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: NoSuchW ...

  6. Delphi--最强大的开发工具(欢迎转载)

    最强大的开发工具 Delphi 目录 --------------------------------------------------------------------------- 前言 De ...

  7. eclipse中tomcat调试正确关联源码

    1.build path中jar包关联本地源码 2.tomcat中添加source关联工程lib下的jar包 以上两步即可. 可解决tomcat直接关联本地源码debug时无法计算表达式的情况. 错误 ...

  8. 我们最常见的UX设计交付成果有哪些?

    以下内容由摹客团队翻译整理,仅供学习交流,摹客iDoc是支持智能标注和切图的产品协作设计神器. 有人会好奇,用户体验(UX)设计师每天都在做些什么呢?说实话,有很多事情!作为UX专家,需要将自己的设计 ...

  9. Two Sum LT1

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  10. linux_关闭防火墙

    centos6版本 永久关闭 chkconfig iptables off 查看状态 chkconfig iptables --list 此时关闭开机重新启动 service iptables sto ...