Integer partitioning problem

Source: Internet
Author: User

Integer division, refers to a positive integer n is written in the following form:

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

If the maximum value of {m1,m2,..., mi} is not greater than m, that is, Max (m1,m2,..., mi) <=m, it is said to belong to a M division of N. Here we remember that the number of M division of N is f (n,m);

For example But when n=4, he had 5 divisions,

4=4,

4=3+1,

4=2+1+1,4=2+2,

4=1+1+1+1.

The problem is to find out the number of all partitions of n, i.e. f (n, N). Here we consider the method of F (n,m);

1. Recursive method:

Depending on the relationship between N and M, consider the following scenarios:

(1) When n=1, no matter what the value of M (m>0), there is only one division of {1};

(2) When m=1, no matter what the value of N, there is only one division of n 1,{1,1,1,..., 1};

(3) When n=m, according to whether the division contains N, can be divided into two situations:

(a) The case where n is included in the division, and only one is {n};

(b) Where the division does not contain n, then the largest number in the division must also be smaller than n, i.e. all (n-1) division of N (F (n,n-1)).

So f (n,n) =1 + f (n,n-1);

(4) When n<m, because there is no possibility of negative numbers in the division, so it is equivalent to F (n,n);

(5) But when n>m, it can be divided into two cases according to whether the maximum value M is included in the division:

(a) the case where M is included in the division, that is {m, {x1,x2,... XI}, where {x1,x2,... XI} and is n-m, so this case

is f (n-m,m)

(b) The division does not contain the case of M, then all the values in the division are smaller than m, i.e. N (m-1), the number is F (n,m-1);

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

Sum up:

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

F (n,m) = f (n, N); (n<m)

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

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

Java implementations:

1  Public classDivideinteger {2     3      Public Static intFintNintm) {4         if(n<1| | M&LT;1)return0;5         if(n==1| | M==1)return1;6         if(n<m)returnf (n,n);7         if(n==m)return1+f (n,n-1);8         returnF (n-m,m) +f (n,m-1);9     }Ten      One      Public Static voidMain (string[] args) { ASystem.out.println (f (6,6)); -     } -      the}

Integer partitioning 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.