Division of Integers (Python)

Source: Internet
Author: User

The division of integers is a classic problem, with many variants. To sum up, there are probably the following variants:

1) divide N into the sum of several positive integers

2) divide N into the number of sums of several different positive integers.

3) divide N into the number of sums with no more than k Positive Integers

4) divide N into the number of bins with no more than k different positive integers

5) divide N into the sum of positive integers whose maximum number is not greater than K

6) divide N into the sum of several consecutive positive integers.

Ohoh, why so many @@

Deformation (1): Divide N into the sum of several positive integers. For example, 6 can be divided as follows:

6
5 + 1
4 + 2, 4 + 1 + 1
3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1

So the answer should be 11.

Analysis:

Based on the relationship between N and M, consider the following situations:

(1) When n = 1, no matter how much m is (M> 0), there is only one division: {1 };

(2) When M = 1, no matter how many N values are, there is only one division of N 1, {, 1,..., 1 };

(3) When n = m, there are two possible conditions:

(A) if the Division contains N, only one is {n };

(B) If n is not included in the Division, the maximum number in the Division must be smaller than N, that is, all (n-1) Division of N.

Therefore, F (n, n) = 1 + f (n, n-1 );

(4) When n <m, it is equivalent to F (n, n) because it cannot be a negative number in the Division );

(5) but n> M, whether or not the maximum M is included in the Division can be divided into two situations:

(A) The Division contains M, that is, {M, {x1, x2 ,... XI }}, {x1, x2 ,... the sum of Xi} is n-M, and m may appear again. Therefore, it is the M division of (n-m,

Therefore, the number of such partitions is F (n-M, M );

(B) if the Division does not contain M, all values in the Division are smaller than m, that is, the number of n (m-1) partitions is F (m-1 );

Therefore, F (n, m) = f (n-M, m) + f (m-1 );

Therefore, if DP [N] [m] is set, n is divided into a maximum of M.

DP [N] [m] = 1 (M = 1)

DP [N] [m] = DP [N] [M-1] + 1 (M = N)

DP [N] [m] = DP [N] [N] (M> N)

DP [N] [m] = DP [N] [M-1] + dp [n-M] [m] (M <n)

Code:

[CPP] view plain
Copy

# Include <cstdio>
# Include <cstring>
# Define maxn500
Using namespace STD;
Int DP [maxn] [maxn];
Int main ()
{
Int I, J;
Memset (DP, 0, sizeof (DP ));
For (I = 1; I <maxn; ++ I)
DP [I] [1] = 1;
For (I = 1; I <maxn; ++ I)
For (j = 1; j <maxn; ++ J ){
If (I> J)
DP [I] [J] = DP [I] [J-1] + dp [I-j] [J];
Else if (I = J)
DP [I] [J] = DP [I] [J-1] + 1;
Else DP [I] [J] = DP [I] [I];
}
}

Deformation (2): dividing N into the sum of several different positive integers toj.3511 Staircases

Still DP, the transfer equation is a [k] [N] = A [k + 1] [N] + A [k + 1] [n-k]; A [k] [N] indicates the division of N into numbers not less than K.

Initialize to a [n] [N] = 1; A [k] [N] = 0 (k> N ).

Code:

[CPP] view plain
Copy

# Include <cstdio>
# Include <cstring>
# Define maxn500
Using namespace STD;
Int DP [maxn] [maxn];
Int main ()
{
Int I, J;
Memset (DP, 0, sizeof (DP ));
For (I = 1; I <maxn; ++ I)
DP [I] [I] = 1;
For (I = 2; I <maxn; ++ I)
For (j = 1; j <I; ++ J)
DP [J] [I] = 0;
For (I = 2; I <maxn; ++ I)
For (j = I-1; j> = 1; -- J)
DP [J] [I] = DP [J + 1] [I] + dp [J + 1] [I-j];
}

Deformation (3): Divide N into the number of sums of no more than k Positive Integers

Solution: DP [I] [J] indicates the number of parts divided by I into J, which is converted to Sigma (DP [N] [I]) (I <= K );

The transfer equation of DP [I] [J] Is DP [I-1] [J-1] + dp [I-j] [J];

See the http://wurong81.spaces.live.com/blog/cns for details! 5eb4a630986c6ecc! 254. Entry

Code:

[CPP] view plain
Copy

# Include <cstdio>
# Include <cstring>
# Define maxn500
Using namespace STD;
Int DP [maxn] [maxn];
Int main ()
{
Int I, J;
// Calculate the number of parts divided into M parts.
For (I = 1; I <maxn; ++ I)
For (j = 1; j <= (I <m? I: m); ++ J)
DP [I] [J] = DP [I-1] [J-1] + dp [I-j] [J];
}

Deformation (4): Divide N into the number of sums of up to k different positive integers

Solution: it is the meaning of the equation of deformation 2. A [k] [N] indicates the number of sums that divide N into numbers not greater than K portions.

Deformation (5 ):

Deformation (6): This is the simplest. N can be written as the sum of consecutive k (k> = 2) positive integers, then K <= SQRT (2 * n );

Enable I to cycle from K to 2. If I is an odd number and N % I = 0, it can be divided into the number of I, and the first number is N/I-I/2;

If I is an even number and N % I = I/2, it can be divided into the number of I, the first number is (N-1)/I-I/2 + 1;

Related Article

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.