Description

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

多重部分和问题:
有n中大小不同的数字,每种c[i]个,判断这些数字之中能否选出若干个使其和为K
此题是让求K<=m时,有多少个解 一个一般性的代码如下
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int n, m;
int a[];
int c[];
int dp[][]; int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
while(scanf("%d %d",&n,&m) != EOF && (n != && m != )) {
for(int i = ; i < n; i++) {
scanf("%d",&a[i]);
}
for(int i = ; i < n; i++) {
scanf("%d",&c[i]);
}
memset(dp, , sizeof(dp));
dp[][] = ;
for(int i = ; i < n; i++) {
for(int j = ; j <= m; j++) {
for(int k = ; k <= c[i] && k * a[i] <= j;k++) {
dp[i+][j] = dp[i+][j]| dp[i][j-k*a[i]];
}
}
}
int ans = ;
for(int i = ; i <= m; i++) {
if(dp[n][i] > ) {
ans++;
}
}
printf("%d\n",ans);
}
return ;
}

若用dp[i+1][j]表示用前i个数相加和为j时第i种数最多能剩余几个(不能得到和为-1)

可得代码如下

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int n, m;
int a[];
int c[];
int dp[]; int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
while(scanf("%d %d",&n,&m) != EOF && (n != && m != )) {
for(int i = ; i < n; i++) {
scanf("%d",&a[i]);
}
for(int i = ; i < n; i++) {
scanf("%d",&c[i]);
}
memset(dp, -, sizeof(dp));
dp[] = ;
for(int i = ; i < n; i++) {
for(int j = ; j <= m; j++) {
if(dp[j] >= ) {
dp[j] = c[i];
}
else if(j < a[i] || dp[j - a[i]] <= ) {
dp[j] = -;
}
else {
dp[j] = dp[j - a[i]] - ;
}
}
}
int ans = ;
for(int i = ; i <= m; i++) {
if(dp[i] >= ) {
ans++;
}
}
printf("%d\n",ans);
}
return ;
}

多重部分和 poj1742的更多相关文章

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

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

  2. POJ_1742_Coins_(动态规划,多重部分和)

    描述 http://poj.org/problem?id=1742 n种不同面额的硬币 ai ,每种各 mi 个,判断可以从这些数字值中选出若干使它们组成的面额恰好为 k 的 k 的个数. 原型: n ...

  3. COJ 0557 4013多重部分和问题

    4013多重部分和问题 难度级别:B: 运行时间限制:2000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 n种大小不同的数字 Ai,每种各Mi个,判断是否可以从 ...

  4. 编程算法 - 多重部分和问题 代码(C)

    多重部分和问题 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有n种不同大小的数字a, 每种各m个. 推断能否够从这些数字之中选出若干使它们的 ...

  5. HDU2844(多重部分和)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. 题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  7. 题解报告:hdu 1059 Dividing(多重背包、多重部分和问题)

    Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...

  8. DP的初级问题——01包、最长公共子序列、完全背包、01包value、多重部分和、最长上升子序列、划分数问题、多重集组合数

    当初学者最开始学习 dp 的时候往往接触的是一大堆的 背包 dp 问题, 那么我们在这里就不妨讨论一下常见的几种背包的 dp 问题: 初级的时候背包 dp 就完全相当于BFS DFS 进行搜索之后的记 ...

  9. POJ1742(多重部分和问题:模板题)

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 32776   Accepted: 11131 Descripti ...

随机推荐

  1. IE兼容只读模式

    表单input具有只读模式属性,一般来说,一般的浏览器都支持该属性,即readyonly,但IE不支持,只能寻找其兼容性. 第一种:unselectable='on' <input id=&qu ...

  2. OpenCV中CvMat的初始化[转]

    一)cvCreateMat创建和分配数据CvCreateMat会创建CvMat,并为CvMat分配数据.cvCreateMat可以配合cvInitMatHeader来初始化CvMat对象.因为CvCr ...

  3. https握手失败案例(一)

      OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .read ...

  4. 更改IDEA默认使用JDK1.5编译项目

    在使用IDEA编译项目时,总是提示我编译失败,有些功能是在1.5之后的版本才支持.废了好大的功夫才编译成功.下面呢,就是更改编译的过程了! 一.初步更改 使用快捷键Ctrl+Alt+Shift+S打开 ...

  5. MYSQL 写入emoji表情字符处理

    这个鬼emoji表情是4个字节,mysql使用的utf8编码,UTF8占3个字节,要存储那个emoji表情需要将mysql编码由UFT8改为UFT8的超集,utf8mb4; 改数据库编码容易引起大面的 ...

  6. OpenGL列主元矩阵和列主序存储

    OpenGL矩阵要考虑两个点,一个是向量如何排布,一个是矩阵如何存储和恢复. 1.排布 排布决定了运算的顺序.OpenGL使用的是列主元,它的意思就是一个4X4的矩阵是由4个列向量构成(这里的v1,v ...

  7. DS博客作业08--课程总结

    DS博客作业08--课程总结 1.当初你是如何做出选择计算机专业的决定的? 1.1 经过一年学习,你的看法改变了么,为什么? 1.2 你觉得计算机是你喜欢的领域吗,它是你擅长的领域吗? 为什么? 1. ...

  8. glove 安装错误

    https://stackoverflow.com/questions/44921611/error-installing-glove-python-link-exe-failed-with-exit ...

  9. 企业版https

    http://www.cocoachina.com/bbs/read.php?tid=194213

  10. NOIP 模拟题

    目录 T1 : grid T2 : ling T3 : threebody 数据可私信我. T1 : grid 题目:在一个\(n*n\)的方格中,你只能斜着走.为了让问题更简单,你还有一次上下左右走 ...