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. mysql-5.7.19免安装版的配置方法

    1. 下载MySQL Community Server 5.6.13 2. 解压MySQL压缩包     将以下载的MySQL压缩包解压到自定义目录下,我的解压目录是:     "D:\Pr ...

  2. TCP Nagle算法&&延迟确认机制

    TCP Nagle算法&&延迟确认机制 收藏 秋风醉了 发表于 3年前 阅读 1367 收藏 0 点赞 0 评论 0 [腾讯云]买域名送云解析+SSL证书+建站!>>> ...

  3. 八皇后问题(dfs)

    #include <iostream> #include <stdio.h> using namespace std; ; ], b[], c[], vis[][]; //a, ...

  4. iOS.ARM-Assembly

    ARM Assembly for iOS with Xcode 0. Introduction 0.1 arm asm vs. arm64(ARMv8) asm AArch64: 0.2 __arm6 ...

  5. CODE[VS]2494 Vani和Cl2捉迷藏

    原题链接 这里有一个结论:最多能选取的藏身点个数等于最小路径可重复点覆盖的路径总数. 所以我们可以先传递闭包,然后求最小路径点覆盖即可. #include<cstdio> #include ...

  6. 没加载redis类,却可以实例化redis

    原因:phpinfo里面已有redis扩展

  7. Increase PHP script execution time with Nginx

    If you have a large WordPress setup or a server with limited resources, then you will often see the ...

  8. Split Array Largest Sum LT410

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. .Net直接将Web页面table导出到Excel

    项目管理系统有个统计表需要导出到Excel表中.常用的方法是在后台C#代码查询数据再写入Excel表中最后保存在目标路径. 为减轻数据库服务器的压力和保持页面的样式,能否直接将页面的表格直接导出到Ex ...

  10. Asp.Net 启用全局IE兼容模式

    Asp.Net 启用全局IE兼容模式,不失为一个种简单最有效的解决方案: <system.webServer> <!-- 配置全局兼容 --> <httpProtocol ...