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)