HDU 2844 Coins Multiple backpack (binary optimized)

Source: Internet
Author: User

Click to open link
CoinsTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 8167 Accepted Submission (s): 3327


Problem descriptionwhuacmers use coins. They has 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 at 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 is 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 and calculate how many prices (Form 1 to m) Tony can pay use these coins.
Inputthe input contains several test cases. The first line of all test case contains, 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 was followed by the zeros.
Outputfor each test case output of the answer on a single line.
Sample Input
3 101 2 4 2 1 12 51 4 2 10 0

Sample Output
84

Source2009 multi-university Training Contest 3-host by WHU
There are n currencies that tell you the value and quantity of each currency, allowing you to ask that these currencies be able to exactly form the number of 1~m cases. multiple Backpack template questions.
499ms1444k#include<stdio.h> #include <string.h> #include <algorithm> #define M 100007using namespace Std;int dp[m],val[107],num[107],m;int n;void zeroonepack (int value) {for (int i=m;i>=value;i--) dp[i ]=max (Dp[i],dp[i-value]+value);} void Completepack (int value) {for (int i=value;i<=m;i++) Dp[i]=max (dp[i],dp[i-value]+value);}    void Multiplepack (int count,int value) {if (count*value>m) completepack (value);        else {int k=1;            while (K<count) {zeroonepack (k*value);            Count-=k;        k<<=1;    } zeroonepack (Count*value);        }}int Main () {while (scanf ("%d%d", &n,&m), n|m) {int ans=0,k=0;        Memset (Dp,0,sizeof (DP));        for (int i=1;i<=n;i++) scanf ("%d", &val[i]);        for (int i=1;i<=n;i++) scanf ("%d", &num[i]);        for (int i=1;i<=n;i++) Multiplepack (Num[i],val[i]);           for (int i=1;i<=m;i++) if (dp[i]==i) ans++;    printf ("%d\n", ans); }}


HDU 2844 Coins Multiple backpack (binary optimized)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.