HDU 1284 coin exchange problem (full backpack: Entry question), hdu1284
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 ;}
Acm hdu2844 coin combination problem. I converted the problem into a 01 backpack, but I don't know why WA ··
The array of w and dp is not big enough. It must be 10 ^ 5 in size.
// TLE # include <iostream> # include <algorithm> using namespace std; # define maxm 100010 # define maxn 101int main (int argc, char * argv []) {int n, m, I, j, count, count1; int v [maxn], num [maxn]; int w [maxm], dp [maxm]; while (scanf ("% d", & n, & m) & (n | m) {I = 0; memset (dp, 0, sizeof (dp); count = count1 = 0; for (I = 0; I <n; I ++) scanf ("% d", & v [I]); for (I = 0; I <n; I ++) scanf ("% d", & num [I]); j = 0; for (I = 0; I <n; I ++) {while (num [I] --) {w [j ++] = v [I]; count1 ++ ;}} for (I = 0; I <count1; I ++) {for (j = m; j> = w [I]; j --) {dp [j] = max (dp [j], dp [j-w [I] + w [I]) ;}} count1 = 0; for (I = 1; I <= m; I ++) {if (dp [I] = I) {count1 ++ ;}} printf ("% d \ n", count1);} return 0 ;}
The correct solution should be the binary optimization of the backpack or the monotonous queue optimization.
It is also possible to use the primary function.
// AC # include <iostream> # include <algorithm> using namespace std; # define maxm 100010 # define maxn 101int main (int argc, char * argv []) {int n, m, I, j, k, count1; int v [maxn], num [maxn]; int dp [maxm]; while (scanf ("% d ", & n, & m) & (n | m) {I = 0; memset (dp, 0, sizeof (dp); for (I = 0; I <n; I ++) scanf ("% d", & v [I]); for (I = 0; I <n; I ++) scanf ("% d", & num [I]); for (I = 0; I <n; I ++) {for (j = m; j> = 0; j -- ) {If (dp [j]! = J) continue; for (k = 1; k <= num [I]; k ++) {count1 = j + k * v [I]; if (count1 ...... remaining full text>