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. kafka基础四

    消费者消费过程(二) 消费组状态机:消息的产生存储消费看似是杂乱无章的,但万物都会遵循一定的规则成长,任何事物的发展都是有迹可循的. 开始消费组初始状态为Stable,经过第一次Rebalance之后 ...

  2. Azure Powershell 获取可用镜像 PublisherName,Offer,Skus,Version

    #登录 $username="{登录名}" #定义一个用户账号的变量,可以输入需要登录的订阅账号名称 $password=ConvertTo-SecureString -Strin ...

  3. RAM建模和初始化

    冯诺依曼提出的存储计算,计算存储,因此,几乎所有的CPU和ASIC都会使用存储器,它们的类型很多,包括异步RAM.同步RAM.ZBT RAM.DDR DRAM.ROM等.由于大部分的异步RAM和SRA ...

  4. [windows]桌面中添加我的电脑,我的文档和网上邻居图标

    xp系统: 操作步骤:桌面任意位置--〉右键--〉属性--〉桌面选项卡--〉自定义桌面--〉常规:勾选相关图标确定即可. win7系统: 操作步骤:桌面任意位置--〉右键--〉个性化--〉(右侧)更改 ...

  5. [Ubuntu]清除系统磁盘垃圾

    操作步骤: 1.sudo apt-get autoremove(卸载系统中所有未被使用的依赖关系) 2.sudo apt-get clean(清除所有缓存的包文件) 以上操作绿色无害,对系统无影响.

  6. jmeter常量吞吐量定时器

    jmeter常量吞吐量定时器

  7. 洛谷 2543 [AHOI2004]奇怪的字符串

    题目描述 输入输出格式 输入格式: 输入文件中包含两个字符串X和Y.当中两字符串非0即1.序列长度均小于9999. 输出格式: X和Y的最长公共子序列长度. 输入输出样例 输入样例#1: 010101 ...

  8. 使用Kubernetes里的job计算圆周率后2000位

    使用Kubernetes里的job(作业),我们可以很方便地执行一些比较耗时的操作. 新建一个job.ymal文件: 定义了一个Kubernetes job,名称为pi,类型为job,容器名称为pi, ...

  9. Js图片缩放代码 鼠标滚轮放大缩小 图片向右旋转

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Bootstrap历练实例:块级按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...