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. JAVA-汉字转换成汉语拼音(pinyin4j-2.5.0-sources.jar)

    在项目中,经常会使用汉语拼音,特别是把汉字转换成汉语拼音.下面给出一种常用的工具. 在使用该程序必须添加 pinyin4j-2.5.0-sources.jar包. import net.sourcef ...

  2. WORD操作的问题

    最近有个小项目主要是对文档,特别是WORD的操作,读取表格数据存到数据库: 再把数据库的数据读出来写入WORD,下载下来,诸如此类的东西,说来很是简单. 想了想是用什么开发呢? C#常用的,没话说,也 ...

  3. jQuery选择器之样式

    .attr()与.removeAttr() 每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息.如:在img元素中,src就是元素的特性,用来标记图片的地址. 操作特性的 ...

  4. SQL Server 2008添加字段成功,但提示列名无效

    在sql后查询,给现有表添加一个字段,即执行sql语句: alter table [sxrq_1108].[dbo].[公司周报表详情] add 总计 int default 0 然后在上述sql查语 ...

  5. 20170308web作业1

    代码20170308001: <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  6. jmeter中文件上传配置

  7. EJB开发基础——EJB规范

    1.EJB 容器          Enterprise Bean 是在称作 EJB 容器的特殊环境中运行的软件组件.容器容纳和管理 Enterprise Bean 的方式与 Java Web 服务器 ...

  8. LINUX提高openfire并发数(网上收集)

    谷歌博客地址:http://tsaiquinn.blogspot.com/2014/10/linuxopenfire.html 影响连接数的元素包含三种:1)Linux的系统参数2)进程自身可以创建的 ...

  9. COGS 2084. Asm.Def的基本算法

    ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] “有句美国俗语说,如果走起来像鸭子,叫起来像 ...

  10. 洛谷 P1732 活蹦乱跳的香穗子

    题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现自己很无聊,于是她想了一个有趣的事情,每个格子最多只能经过1次,且每个格子都有其价值 跳的规则是这样的,香穗子可以向上下左右四个方向跳到相邻的格子,并且她 ...