HDU 2844 Coins (Multi-pack counting space Change time)

Source: Internet
Author: User


Coins Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 8999 Accepted Submission (s): 3623
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.  OutputThe For each test case output The answer to a single line. Sample Input
3 101 2 4 2 1 12 51 4 2 10 0
 Sample Output
84
 Sourcemulti-university Training Contest 3-host by WHU

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2844

The main topic: N Kinds of numbers, each number of the value of AI, there are CI, how many can be composed of not more than m of different numbers

Topic Analysis: The data volume of the problem is really moving, three-layer loop (enumeration type, enumeration value, enumeration number) must time out, to find a way to subtract a layer of loops, careful analysis can be found that the number of the layer of the cycle can be omitted, the cost is to spend more space, open a CNT array record the current number of I used, The other two-layer loop is constant, then the normal backpack counting problem

#include <cstdio> #include <cstring>int const MAXM = 1e5 + 5;int Const MAXN = 1e2 + 5;bool dp[maxm];int Cnt[max M];int A[MAXN], c[maxn];int  main () {    int n, m;    while (scanf ("%d%d", &n, &m)! = EOF && (n + m))    {        memset (DP, FALSE, sizeof (DP));        for (int i = 1; I <= n; i + +)            scanf ("%d", &a[i]);        for (int i = 1; I <= n; i + +)            scanf ("%d", &c[i]);        Dp[0] = true;        int ans = 0;        for (int i = 1; I <= n; i + +)        {            memset (CNT, 0, sizeof (CNT));            for (int s = a[i]; s <= m; s + +)            {                if (!dp[s] && dp[s-a[i] [&& Cnt[s-a[i]] < c[i])                {                    Dp[s] = true;                    Ans + +;                    Cnt[s] = Cnt[s-a[i]] + 1;        }} printf ("%d\n", ans);}    }


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2844 Coins (Multi-pack counting space Change time)

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.