HDU---2069 (generating function 2D and 3D DP)

This problem can be a generating function or DP.

Generating function:

In order to describe that the number of money in each retrieval method should be limited to 100100100, then the arrays of sup[]sup[]sup [] and temp [] temp [] add another one dimension, indicating that the number of money in this state is iii. then, when the polynomials are combined, it is determined whether it is more than 100100100. If it is more than 100100, it is not counted.

DP:

In the same way, the general DP[i][j]DP[i][j]DP[i][j] represents the scheme number of the first class iii cents, which can reach the money number jjj.

In order to limit the number of coins to no more than 100100100, a state and a dimension are added.

There is a set of dp[i][j][k]dp[i][j][k]dp[i][j][k] to represent the first class iii cents, and the number of schemes that can reach the number of money kkk by using jjj coins.

Since there is no limit to the number of money in each type, DP is carried out in the form of complete backpack. DP. DP.

1. If none of the current coins can be loaded, the number of schemes remains the same, dp[i − 1][j][k]. dp[i-1][j][k]. dp[i − 1][j][k].

2. If the current coins are enough for one or more, according to the complete knapsack, for the coins from this layer (starting from this kind of coins), and then take them all the time, if you can take them (just like the complete knapsack), then there is dp[i][j − 1][k − a[i]]dp[i][j-1][k-a[i]]dp[i][j − 1][k − a[i]], pay attention to take one at a time, so the number is minus 1. 1. 1.

The above is 3D DP 3D DP 3D DP state transition, and then it is found that it is similar to the complete knapsack, and can be reduced to 2D by dp[i − 1]dp[i-1]dp[i − 1].

Here is the code for the generating function, 3D DP and 2D DP:

Generating function:

#include<iostream>
#include<algorithm>
#include<string.h>
#define maxn 5008
using namespace std;
int a[6] = { 0,1,5,10,25,50 };
int n;
int sup[101][maxn], temp[101][maxn];
int main()
{
    while (~scanf_s("%d", &n))
    {
        memset(sup, 0, sizeof(sup));
        memset(temp, 0, sizeof(temp));
        for (int i = 0; i <= min(n, 100); i++) {
            sup[i][i] = 1;
        }
        for (int i = 2; i <= 5; i++) {
            for (int j = 0; j <= n; j++) {
                for (int k = 0; k + j <= n; k += a[i]) {
                    for (int r = 0; r <= 100; r++) {
                        if (r + k / a[i] > 100) break;
                        temp[r + k / a[i]][j+k] += sup[r][j];
                    }
                }
            }
            for (int j = 0; j <= n; j++) {
                for (int k = 0; k <= 100; k++) {
                    sup[k][j] = temp[k][j];
                    temp[k][j] = 0;
                }
            }
        }
        int ans = 0;
        for (int i = 0; i <= 100; i++) {
            ans += sup[i][n];
        }
        printf("%d\n", ans);
    }
}

Three dimensional DP:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int a[6] = { 0,1,5,10,25,50 };
int dp[6][101][300];
int n; 
int main()
{
	while (~scanf("%d", &n))
	{
		memset(dp, 0, sizeof(dp));
		for (int i = 0; i <= 5; i++) {
			dp[i][0][0] = 1;
		}
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= 100; j++) {
				for (int k = 1; k <= n; k++) {
					dp[i][j][k] = dp[i - 1][j][k];
					if (k >= a[i])
						dp[i][j][k] += dp[i][j - 1][k - a[i]];
				}
			}
		}
		int ans = 0;
		for (int i = 0; i <= 100; i ++ ) {
			ans += dp[5][i][n];
		}
		printf("%d\n",ans);
	}
}

Two dimensional DP:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int a[6] = { 0,1,5,10,25,50 };
int dp[101][300];
int n; 
int main()
{
	while (~scanf("%d", &n))
	{
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= 100; j++) {
				for (int k = a[i]; k <= n; k++) {
					dp[j][k] += dp[j - 1][k - a[i]];
				}
			}
		}
		int ans = 0;
		for (int i = 0; i <= 100; i++) {
			ans += dp[i][n];
		}
		printf("%d\n", ans);
	}
}

Posted by thefreebielife on Sun, 20 Oct 2019 09:25:16 -0700