--- Integer partitioning report ---- by debugcool
-------------------------------------------------------
1. Problem description:
Given a positive integer N and K
1.> divide N into the sum of several positive integers.
2.> divide N into the sum of K positive integers.
3.> divide N into partitions whose maximum number cannot exceed K.
4.> divide N into the sum of odd positive integers.
5.> divide N into the sum of several different integers.
2. Problem classification: In general, these are all problems with backpacks;
The first question is: a full backpack with 1-N type. The weight of the I type is I and the value is I. This is a good solution:
DP [0] = 1;
For (I = 1; I <= N; I ++)
For (j = I; j <= N; j ++)
DP [J] + = DP [J-I];
Where DP [J] is the number of types where the former I can constitute J, the result is DP [N]
After reading this question, we will know the 3rd questions, that is, the result of loading the first K backpacks, you only need to change the I <= n of the first loop to I <= K;
DP [0] = 1;
For (I = 1; I <= K; I ++)
For (j = I; j <= N; j ++)
DP [J] + = DP [J-I]; the result is also DP [N];
So the fourth question, think about it as an odd number, then I = 2, 4, 6 ,...... Wait for the value to be removed, because the type of these backpacks is not required, it is very easy to change I ++ to I + = 2;
DP [0] = 1;
For (I = 1; I <= N; I + = 2)
For (j = I; j <= N; j ++)
DP [J] + = DP [J-I]; the result is also DP [N];
Let's take a look at the fifth question. There are several different ??? What do you think ??? What is this? The classic 01 backpack. the difference from the first question is the order of the second loop;
DP [0] = 1;
For (I = 1; I <= N; I ++)
For (j = N; j> = I; j --)
DP [J] + = DP [J-I];
Finally, let's think about the second question:
If K is required, what should we do ??? Think about special situations ...... If K is 1, it can only be n. If n is 0, the result can only be 0. If n is equal to K, the result cannot be 0, well, we have considered whether there is 1 in the score. If so, we will divide the remaining n-1 into k-1 parts. If not, then, let's take out K parts for each heap to 1, and then divide the remaining n-k into k parts. Well, now, the recursive method comes out:
Int work (int n, int K)
{
If (k = 1)
Return 1;
If (n = 0)
Return 0;
If (n <K)
Return 0;
Return work (n-k, k) + work (n-1, k-1 );
}
--------------------------------
Summary: The problem with backpacks is the foundation ~~~
Debugcool ---------------