Hdu 1284 coin Exchange question (Full backpack: entry questions) __ACM

Source: Internet
Author: User

Hdu 1284 coin Exchange question (Full backpack: entry questions)

http://acm.hdu.edu.cn/showproblem.php?pid=1284

The following:

In a country there are only 1 cents, 2 cents, 3 cents, and there are many ways to convert money N (n<32768) into coins. Please write a procedure to figure out how many kinds of methods are in common.

Analysis: The basics of the full knapsack problem.

The limiting condition is: the sum of money <=n.

The objective condition is: To find the number of construction methods.

The DP[I][J]==X said that there were X methods in the construction of J cents in the former I coin.

Initialization: DP is full 0 and dp[0][0]==1.

State transfer: Dp[i][j] = SUM (Dp[i-1][j], dp[i][j-val[i])

Sum is sum, val[i] is the nominal value of the first coin. The above equation refers to the first value of a coin is not selected, the latter refers to the selection of at least 1 of the first coins.

The final request: Dp[3][n]. The program uses a scrolling array to implement, so the DP has only [j] this dimension.

AC Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=40000+5;

int n;
Long long DP[MAXN];

int main ()
{
    //Initialize
    memset (dp,0,sizeof (DP));
    Dp[0]=1;

    Recursive for (
    int i=1;i<=3;i++) for
        (int j=i;j<maxn;j++)
            dp[j] + = dp[j-i];

    Output results
    while (scanf ("%d", &n) ==1)
        printf ("%i64d\n", Dp[n));
    return 0;
}

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.