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

Coins

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

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
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=100005;
int dp[MAXN];
int n,m;
int A[MAXN];
int C[MAXN];
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        memset(dp,-1,sizeof(dp));
        for(int i=0;i<n;i++)    scanf("%d",&A[i]);
        for(int i=0;i<n;i++)    scanf("%d",&C[i]);
        dp[0]=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<=m;j++)
                if(dp[j]>=0)
                {
                    dp[j]=C[i];
                }
                else if(j<A[i]||dp[j-A[i]]<=0)
                {
                    dp[j]=-1;
                }
                else
                {
                    dp[j]=dp[j-A[i]]-1;
                }
        int ans=0;
        for(int i=1;i<=m;i++)
            if(dp[i]>=0)    ans++;
        printf("%d\n",ans);
    }

    return 0;
}
时间: 2024-10-19 16:41:00

POJ1742(多重部分和问题:模板题)的相关文章

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

原题链接:http://poj.org/problem?id=1742 题目大意:tony现在有n种硬币,第i种硬币的面值为A[i],数量为C[i].现在tony要使用这些硬币去买一块价格不超过m的表.他希望买的时候不用找零,问有多少种价格能满足这一点. 这个问题实际上是一个多重部分和的问题:假设有n种物品,每种物品的价值为v[i],数量为c[i],随意选取这些物品,能否使它们的价值之和恰好为m.使用动态规划的思想来求解这类问题: 定义dp数组,dp[i][j]的值代表前i种物品随意选取,价值之

多重部分和 poj1742

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 pri

hdu 2844 Coins 多重背包模板题 ,二进制优化。据说是楼教主的男人八题之一

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8052    Accepted Submission(s): 3291 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One

COJ 0557 4013多重部分和问题

4013多重部分和问题 难度级别:B: 运行时间限制:2000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 n种大小不同的数字 Ai,每种各Mi个,判断是否可以从这些数字之中选出若干个使他们的和恰好为K. 输入 第一行为两个正整数n,K.第二行为n个数Ai,以空格隔开.第三行为n个数Mi,以空格隔开. 输出 若可行则输出"yes"否则输出"no" 输入示例 3 173 5 83 2 2 输出示例 yes 其他说明 1<=n

hdu 2966 In case of failure kdtree模板题

问求每个点距离平方的最小的点 kd-tree模板题…… 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(x) cerr<<#x<<"=="<<(x)<<endl 4 using namespace std; 5 typedef long long ll; 6 typedef pair<int,int>

几道树剖模板题

寒假后半段一直都在外出旅游..颓了好久..qaq 旅游期间写了几道树剖模板题,贴上来.. BZOJ 1036 没啥好说的,裸题 1 #include <cstdio> 2 #include <algorithm> 3 4 #define LEFT (segt[cur].l) 5 #define RIGHT (segt[cur].r) 6 #define MID (segt[cur].mid) 7 #define SUM (segt[cur].Sum) 8 #define MAX (

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

HUST 1017 - Exact cover (Dancing Links 模板题)

1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows. Try to find o

洛谷P3381——费用流模板题

嗯..随便刷了一道费用流的模板题....来练练手. #include<iostream> #include<cstdio> #include<cstring> using namespace std; int h[5210],d[5210],used[5210],que[100010],last[5210]; int k=1,INF=0x7fffffff,ans1=0,ans2=0; inline int read(){ int t=1,num=0; char c=ge