C ++: recursive algorithm Integer Division

Source: Internet
Author: User
Tags integer division

The integer division problem is one of the classical propositions in the algorithm. The explanation of this problem involves the basics of recursion. The so-called Integer Division refers to writing a positive integer N as follows:

N = m1 + M2 +... + Mi; (where Mi is a positive integer and 1 <= mi <= N), then {M1, M2 ,..., mi} is a division of N.

If {M1, M2 ,..., the maximum value in mi} cannot exceed M, that is, Max (M1, M2 ,..., mi) <= m, it is called an M division of N. Here we will record the number of M divisions of N as F (n, m );

For example, when n = 4, there are five divisions: {4}, {, 1 };

Note that 4 = 1 + 3 and 4 = 3 + 1 are considered to be the same division.

This problem is used to obtain the number of all N partitions, that is, F (N, N ). Next we will consider the F (n, m) method;

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:

(). 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, this Division

The number 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 division of n (m-1), and the number is F (m-1 );

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

 

Based on the above situation, we can see that the above conclusions have recursive defining features, where (1) and (2) belong to regression conditions, (3) and (4) are special situations, will be converted to the case (5 ). Situation (5) is a general situation and is a recursive method. In essence, it mainly solves the problem by reducing m to reach the regression condition. The recursive expression is as follows:

F (n, m) = 1; (n = 1 or M = 1)

F (n, n); (n <m)

1 + f (N m-1); (n = m)

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

Some code is as follows:

unsigned long  GetPartitionCount(int n, int max){    if (n == 1 || max == 1)        return 1;    else if (n < max)        return compute(n, n);    else if (n == max)        return 1 + GetPartitionCount(n, max-1);    else        return GetPartitionCount(n,max-1) + GetPartitionCount(n-max, max);}

 

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.