Solution idea: Because the state transfer equation for the complete backpack F[v]=max (F[v],f[v-c[i]]+w[i]) has documented all of the backpack composition scheme, but usually ask for the maximum value, now requires the total number of programs
That is F[v]=sum (F[v],f[v-c[i]+w[i]]),
Problem description in a country only 1 points, 2 points, 3 cent coins, the exchange of money n into coins there are many kinds of laws. Please compile the procedure to calculate the total number of different methods. Input has only one positive integer n,n less than 32768 per line. Output corresponds to each input and the number of redemption methods. Sample Input2934 12553 Sample Output718831 13137761
#include <stdio.h> #include <string.h>long long f[50000];int a[4];long long sum (long long A,long long b) { return a+b;} int main () { int n,i,v; while (scanf ("%d", &n)!=eof) { memset (f,0,sizeof (f)); F[0]=1; A[1]=1; a[2]=2; a[3]=3; for (i=1;i<=3;i++) {for (v=a[i];v<=n;v++) f[v]=sum (F[v],f[v-a[i]]); } printf ("%i64d\n", F[n]); } }
Hangzhou Electric 1284 Coin Exchange Problem "total Knapsack solution"