hdu 2844 Coins DP

Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7716    Accepted Submission(s): 3158

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
#include<iostream>
#include<cstring>
using namespace std;
int dp[100050],c[100050];

int main(){
    int n,m;
    while(cin>>n>>m && (n||m)){
        int i,j;
        int u[105],v[105];
        for(i=0;i<=m;i++)  dp[i]=0;
        for(i=1;i<=n;i++)   cin>>u[i];
        for(i=1;i<=n;i++)   cin>>v[i];

        /*int r=1;
        for(i=1;i<=n;i++){
            for(j=1;j<=v[i];j<<=1){
                c[r++]=j*u[i];
                v[i]-=j;
            }
            if(v[i]>0)  c[r++]=v[i]*u[i];
        }
        for(i=1;i<r;i++)
            for(j=m;j>=[i];j--)
                dp[j]=max(dp[j],dp[j-c[i]]+c[i]);*/
        for(i=1;i<=n;i++){
            for(int k=0;k<=m;k++)   c[k]=0;
            for(j=u[i];j<=m;j++)
            if(dp[j]<dp[j-u[i]]+u[i] && c[j-u[i]]<v[i]){
                dp[j]=dp[j-u[i]]+u[i];
                c[j]=c[j-u[i]]+1;
            }
        }

        int ans=0;
        for(i=1;i<=m;i++)
            if(dp[i]==i)  ans++;
        cout<<ans<<endl;

    }
    return 0;
}
时间: 2024-11-11 00:45:50

hdu 2844 Coins DP的相关文章

HDU 2844 Coins (动规)

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

HDU 2844 Coins

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(witho

背包系列练习( hdu 2844 Coins &amp;&amp; hdu 2159 &amp;&amp; poj 1170 Shopping Offers &amp;&amp; hdu 3092 Least common multiple &amp;&amp; poj 1015 Jury Compromise)

作为一个oier,以及大学acm党背包是必不可少的一部分.好久没做背包类动规了.久违地练习下-.- dd__engi的背包九讲:http://love-oriented.com/pack/ 鸣谢http://blog.csdn.net/eagle_or_snail/article/details/50987044,这里有大部分比较有趣的dp练手题. hdu 2844 Coins 多重背包 就是一个10w的多重背包,每个物品的cost同时也作为value去做背包,我们求的是每个容量下的价值,所以没

HDU 2844 Coins (组合背包)

题意   给你n种面额不同的金币和每种金币的个数  求这些金币能组合成的面额在m内有多少种 还是明显的背包问题  d[i]表示这些金币在i内能组合成的最大面额  初始化d为负无穷  d[0]=0  这样就可以保证d[i]恰好为i时才能为正值 原因可以自己想想  然后就用背包背吧  直接多重背包也可以过  但是分成多重背包和完全背包要快一点 #include<cstdio> #include<cstring> #include<algorithm> using names

HDU 2844 Coins (多重背包问题DP)

题意:给定n种硬币,每种价值是a,数量是c,让你求不大于给定V的不同的价值数,就是说让你用这些硬币来组成多少种不同的价格,并且价格不大于V. 析:一看就应该知道是一个动态规划的背包问题,只不过是变形,那我们就统计不大于V的不同价格数,也容易实现, 对于多重背包我们是把它转化为01背包和完全背包来解决的. 代码如下: #include <cstdio> #include <iostream> #include <cstring> using namespace std;

HDU 2844 Coins (多重背包计数 空间换时间)

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

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

HDU 2844 Coins 多重背包(二进制优化)

点击打开链接 Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8167    Accepted Submission(s): 3327 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dolla

hdu 2844 coins (多重背包)

#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int a[120],c[120]; int dp[100000+100]; int main() { int n,m; int i,j,k; while(scanf("%d%d",&n,&m