HDU 1284 coin exchange problem mother function, DP

Source: Internet
Author: User

Link: HDU 1284 coin exchange

Coin Exchange Problems Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 5467 accepted submission (s): 3123


Problem description has only one cent, two cent, and three cent coins in a country. There are many exchange methods to convert money N into coins. Compile a program to calculate the total number of exchange methods.
Each input row has only one positive integer N, and N is less than 32768.
Output corresponds to the number of exchange methods for each input.
Sample Input
293412553
 
Sample output
71883113137761
 
Authorsmallbeer (CRF)
Source hangdian ACM training team training competition (VII)
Recommendlcy | we have carefully selected several similar problems for you: 2159 1248 1203 1231

Idea 1: Evaluate the coefficient of the primary function



Code:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 32770int n, c1[maxn], c2[maxn];void Init(){    for(int i = 0; i <= maxn; i++)    {        c1[i] = 1;        c2[i] = 0;    }    for(int i = 2; i <= 3; i++)    {        for(int j = 0; j <= maxn; j++)            for(int k = 0; k+j <= maxn; k+=i)                c2[j+k] += c1[j];        for(int j = 0; j <= maxn; j++)        {            c1[j] = c2[j];            c2[j] = 0;        }    }}int main(){    Init();    while(~scanf("%d", &n))        printf("%d\n", c1[n]);    return 0;}


Idea 2: DP. The money in the future can be introduced by the money in the front.

Code:

#include <iostream>#include <cstdio>using namespace std;#define maxn 32770int n, dp[maxn];void Init(){    dp[0] = 1;    for(int i = 1; i <= 3; i++)        for(int j = i; j <= maxn; j++)            dp[j] += dp[j-i];}int main(){    Init();    while(~scanf("%d", &n))        printf("%d\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.