Hdu 1284 coin Exchange question (Full backpack: entry questions)
http://acm.hdu.edu.cn/showproblem.php?pid=1284
The following:
In a country there are only 1 cents, 2 cents, 3 cents, and there are many ways to convert money N (n<32768) into coins. Please write a procedure to figure out how many kinds of methods are in common.
Analysis: The basics of the full knapsack problem.
The limiting condition is: the sum of money <=n.
The objective condition is: To find the number of construction methods.
The DP[I][J]==X said that there were X methods in the construction of J cents in the former I coin.
Initialization: DP is full 0 and dp[0][0]==1.
State transfer: Dp[i][j] = SUM (Dp[i-1][j], dp[i][j-val[i])
Sum is sum, val[i] is the nominal value of the first coin. The above equation refers to the first value of a coin is not selected, the latter refers to the selection of at least 1 of the first coins.
The final request: Dp[3][n]. The program uses a scrolling array to implement, so the DP has only [j] this dimension.
AC Code:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn=40000+5;
int n;
Long 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 results
while (scanf ("%d", &n) ==1)
printf ("%i64d\n", Dp[n));
return 0;
}