POJ 3181 Dollar Dayz 01 full backpack problem, pojdayz
01 A complete backpack problem.
This mainly describes the number of combinations. There are more people doing two-dimensional dp. here we can use one-dimensional dp.
One-dimensional transformation equation: dp [j] = dp [j-I] + dp [j]; where I represents the weight and j represents the current backpack capacity.
This means that dp [j-I] indicates the maximum combination of j-I backpack weights. If the size of the backpack is j, that is, you can pack the I-th item into a backpack, so there is a dp [j-I] installation method,
If there is no I item, when the capacity is j, the combination number is dp [j];
When I items exist and the capacity is j, the combination number is dp [j-I] + dp [j];
The features that can be converted from two-dimensional to one-dimensional dp:
1. You do not need to use the data before the current row. when filling out a table, you must first fill in the column and then enter the next row. when entering the current row, you only need to record the data in one row; the data in the current column can be read immediately and overwritten immediately.
2. You can also enter the table in reverse order. When you need to use the data of the first few columns of the current row, you can consider entering the table from the following columns.
However, this question adds a knowledge point, that is, to process large numbers. It is also possible to use a general array for processing. However, based on the characteristics of this question, only two long strings can be used to store the results.
#include <stdio.h>#include <vector>#include <string.h>#include <algorithm>#include <iostream>#include <string>#include <limits.h>#include <stack>#include <queue>#include <set>#include <map>using namespace std;const int MAX_N = 1001;int N, K;long long hi[MAX_N], lo[MAX_N];const long long MOD = 1E18;int main(){while (~scanf("%d %d", &N, &K)){memset(hi, 0LL, sizeof(long long) * (N+1));memset(lo, 0LL, sizeof(long long) * (N+1));lo[0] = 1LL;for (int i = 1; i <= K; i++){for (int j = i; j <= N; j++){hi[j] = hi[j-i] + hi[j];hi[j] += (lo[j-i] + lo[j]) / MOD;lo[j] = (lo[j-i] + lo[j]) % MOD;}}if (hi[N] > 0LL) printf("%I64d", hi[N]);printf("%I64d\n", lo[N]);}return 0;}