HDU 1284 coin exchange problem (full backpack: Getting Started)

Source: Internet
Author: User

HDU 1284 coin exchange problem (full backpack: Getting Started)

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1284

Question:

In a country with only one cent, two cent, and three cent coins, there are many exchange methods to convert money n (n <32768) into coins. Compile a program to calculate the total number of exchange methods.

Analysis: Basic full backpack problems.

This questionRestrictionsYes: Total Amount of money <= n.

This questionTarget ConditionYes: calculates the number of constructor methods.

For example, DP [I] [J] = X indicates that J cents can be constructed using the first I coins in a total of X methods.

Initialization: DP is all 0 and DP [0] [0] = 1.

State Transfer: DP [I] [J] = sum (DP [I-1] [J], DP [I] [J-Val [I])

Sum is the sum, and Val [I] is the nominal value of the I-th coin. the preceding equation indicates that no I-value coin is selected, and the latter indicates that at least one I-value coin is selected.

The final requirement is: DP [3] [N]. The program is implemented using a rolling array, so DP only has one dimension: [J.

AC code:

# Include <cstdio> # include <algorithm> # include <cstring> using namespace STD; const int maxn = 40000 + 5; int N; 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 result while (scanf ("% d", & n) = 1) printf ("% i64d \ n", DP [N]); Return 0 ;}

HDU 1284 coin exchange problem (full backpack: Getting Started)

Related Article

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.