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. Vue 局部组件和全局组件的使用

    <template> <div id="app"> <!--<img alt="Vue logo" src="./ ...

  2. Javascript 四种输出方式

    JavaScript 输出 javascript 没有任何打印或输出的函数 可以通过不同的方式输出数据 使用window.alert() 弹出警告框 使用document.write()方法将内容写到 ...

  3. PHP编程时的规范化命名

    要想成为一名“合格”的程序员,就必须要有良好的编程习惯和规范,这样做的好处有很多,诸如:可以提高代码质量,提高程序的可维护性,提高开发速度和效率等.以下就简要的列出几条日常编写程序时大概要注意的一些“ ...

  4. BZOJ1855 股票交易 单调队列优化 DP

    描述 某位蒟佬要买股票, 他神奇地能够预测接下来 T 天的 每天的股票购买价格 ap, 股票出售价格 bp, 以及某日购买股票的上限 as,  某日出售股票上限 bs, 并且每次股票交 ♂ 易 ( 购 ...

  5. python 计时程序运行时间

    import time time_start=time.time() time_end=time.time() print('totally cost',time_end-time_start)

  6. mysql 设置用户并授权

    一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...

  7. ES开发的一些坑(一)

    一.ES-Hadoop导数据的时候报"Could not write all entries"异常  ES-Hadoop是一个开源的数据导入项目,支持数据从hdfs,hive,sp ...

  8. 多个tomcat shutdown.sh 导致无法正常关闭的问题

    1. 今天启动两个tomcat , 但是由于个人失误,只改了以下两个端口 ,忘记修改shutdown相应端口.这是启动两个tomcat ,可以正常启动并访问.. <Connector port= ...

  9. CXF wsdl2java (转载)

    2011-03-28 14:27 9735人阅读 评论(2) 收藏 举报 servicewebserviceinterfacejavastringserver CXF wsdl2Java 一.  简介 ...

  10. 【C#】解析C#中JSON.NET的使用

    目录结构: contents structure [-] JSON.NET简介 Serializing and Deserializing JSON Json Convert Json Seriali ...