Coins

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 91   Accepted Submission(s) : 36

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse 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

Source

2009 Multi-University Training Contest 3 - Host by WHU 
 
       题目的意思是在1到m,有多少面值可以被已有的硬币凑成。
      比如例2,拥有2种硬币,1面值的2个,4面值的1个,在1到5内可以凑的有1,2,4,5共4个数字,所以输出4。
      思路是用前一状态推后一个状态,比如要凑j(j<=m),如果j-v[i]可以被凑成,那么加上当前的v[i],j也是可以凑成,但是要注意当前凑j所用的 当前硬币个数为  凑j-v[i]的硬币个数+1。凑j所用的 当前硬币个数<=c[i]
 
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
int n,m;
int v[],c[];
int dp[];
int cnt[];//记录当前已用的硬币个数
while(cin>>n>>m)
{
if(m==&&n==) break;
int i,j,k;
memset(dp,,sizeof(dp));
dp[]=;//因为,比如,硬币面值为2,此时dp[2]!=0&&dp[2-2]==1
for(i=;i<=n;i++)
{
cin>>v[i];
}
for(i=;i<=n;i++)
{
cin>>c[i];
}
int cnts=;
for(i=;i<=n;i++)//每一种硬币遍历
{
memset(cnt,,sizeof(cnt));//因为只针对当前硬币
for(j=v[i];j<=m;j++)
{
if(dp[j]!=&&dp[j-v[i]]==&&cnt[j-v[i]]<=c[i]-)
{//价值j还没能被凑成,但j-v[i]可以凑成,且当前所用硬币个数+1<=c[i]
dp[j]=;
cnt[j]=cnt[j-v[i]]+;//在凑成j-v[i]的基础上多用了一个当前的硬币
//如果凑成j-v[i]用了1个当前硬币,j就要用2个
cnts++;//种数++
}
}
}
cout<<cnts<<endl;
}
return ;
}

DP Coins hdoj的更多相关文章

  1. DP:Coins(POJ 1742)

      用硬币换钱 题目大意:就是有面值为A1,A2,A3....的硬币,各有C1,C2,C3...的数量,问在钱数为m的范围内,能换多少钱?(不找零) 这题看名字就知道是完全背包,但是这题又有点不一样, ...

  2. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. 【HDOJ】【4336】Card Collector

    概率DP/数学期望/状压DP/容斥原理 kuangbin总结中的第14题 好神奇的做法……题解看kuangbin的代码好了…… //HDOJ 4336 #include<cstdio> # ...

  4. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  5. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  6. [Swift]LeetCode518. 零钱兑换 II | Coin Change 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  7. [LeetCode] 518. Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  8. LeetCode之找零钱

    题目:已知不同面值的钞票,求如 何用最少数量的钞票组成某个金额,求可 以使用的最少钞票数量.如果任意数量的已知面值钞票都无法组成该金额, 则返回-1. 示例: Input: coins = [1, 2 ...

  9. LeetCode.518 零钱兑换Ⅱ(记录)

    518题是背包问题的变体,也称完全背包问题. 解法参考了该篇文章,然后对自己困惑的地方进行记录. 下面是该题的描述: 有一个背包,最大容量为 amount,有一系列物品 coins,每个物品的重量为 ...

随机推荐

  1. python3 win10_x64 安装pcapy

    稀里糊涂就装上了 大概记录一下都有什么. visual studio build tools 2015 下载地址: http://landinghub.visualstudio.com/visual- ...

  2. Linux 系统信息查询

    众所周知,Linux大部分操作是通过命令实现的,并不像windows那么直观.linux查看硬件信息也是需要通过linux查看硬件信息命令查询的,下面小编来分享一下linux查看硬件信息的方法和命令写 ...

  3. WebStrom 多项目展示及vuejs插件安装

    2. Vuejs 插件安装: ① ②

  4. IOC和DI 控制反转和依赖注入

    首先要分享的是Iteye的开涛这位技术牛人对Spring框架的IOC的理解,写得非常通俗易懂,以下内容全部来自原文,原文地址:http://jinnianshilongnian.iteye.com/b ...

  5. Tensorflow 解决MNIST问题的重构程序

    分为三个文件:mnist_inference.py:定义前向传播的过程以及神经网络中的参数,抽象成为一个独立的库函数:mnist_train.py:定义神经网络的训练过程,在此过程中,每个一段时间保存 ...

  6. [LeetCode&Python] Problem 700. Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  7. freemarker在js中的应用

    <script type="text/javascript"> //freemarker在js中的应用: var newOrganizations = []; < ...

  8. jq 分页

    转自:https://www.cnblogs.com/wolflower/p/6898479.html参考:https://github.com/tianxiangbing/paging先引入这两个插 ...

  9. mysql四种修改密码的方式

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  10. RTSP HTTP RTP RTCP

    RTSP简介 RTSP(Real Time Streaming Protocol)是由Real Network和Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议.RTSP对流 ...