Question: uva674-coin change (full backpack)
Give 1 5 10 25 50 coins with the nominal value of 5, then give n, and ask the number of different ways to make up n with the money. 1 5 and 5 1 indicate that the order is the same.
Solution: A full backpack. State equation: DP [J] + = DP [J-V [I ]];
Code:
# Include <cstdio> # include <cstring> const int n = 5; const int maxn = 8000; typedef long ll; int N; int V [N] = {1, 5, 10, 25, 50}; // ll DP [maxn] [N]; ll d [maxn];/* void Init () {memset (DP,-1, sizeof (DP); // have not been searchfor (INT I = 0; I <n; I ++) DP [0] [I] = 1 ;} * // * ll dp (int x, int K) {ll & Ans = DP [x] [k]; If (ANS! =-1) return ans; ans = 0; For (INT I = K; I <n & x> = V [I]; I ++) ans + = dp (X-V [I], I); Return ans;} */INT main () {// Init (); memset (D, 0, sizeof (d); D [0] = 1; for (INT I = 0; I <n; I ++) for (Int J = V [I]; j <= maxn; j ++) d [J] + = d [J-V [I]; while (scanf ("% d", & N )! = EOF) {// printf ("% LLD \ n", dp (n, 0); printf ("% LLD \ n", d [N]);} return 0 ;}