Poj 3181 dollar dayz 01 full backpack Problem

Source: Internet
Author: User
Tags dayz

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;}




Poj 3181 dollar dayz 01 full backpack Problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.