Algorithm notes-integer Division 1

Source: Internet
Author: User

Title Source: NYOJ90

Problem Description:

represents a positive integer n as the sum of a series of positive integers: n=n1+n2+...+nk, where n1≥n2≥ ... ≥nk≥1,k≥1. This representation of a positive integer n is called a division of positive integer n. The number of non-identical divisions of a positive integer n is obtained .
For example a positive integer 6 has the following 11 different divisions:
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.

Input:

The first line is the number of test data M (1<=m<=10). Each of the following lines contains an integer n (1<=n<=10).

Output:

How many different types of test data are output per group.

Analysis:

  For integer partitioning, the equivalent of a positive integer n is written in the following form:

N=n1+n2+...+nk (where n≥n1≥n2≥ ... ≥nk≥1,k≥1), {n1, N2, ..., NK} is a division of N.

It is assumed that N1 <= m, i.e. {n1, N2, ..., the largest number in NK} is not more than M, then {n1, N2, ...., NK} is a M-upper division of N. The M upper limit of the memory n is divided into F (n,m). Therefore, the solution of this problem is F (n,n). For f (n,m), the following recursive relationship is available for analysis:

1, when n = 1 o'clock, f (n,m) = 1. That is, 1 has only {1} This kind of division.

2, when m = 1 o'clock, f (n,m) = 1. That is, at this point only {1, 1,......, 1} (total n 1) This kind of division.

3, when m = N, f (n,m) = f (n,n) = f (n, n-1) + 1. where "+1" corresponds to {n} This division, the other division, must all the number is less than N.

4, when M > N, f (n,m) = f (n,n), because n is not possible to exist in the division of a number greater than N.

5, when M < N, f (n,m) = f (n-m, M) + f (n, m-1). where F (n-m,m) represents a division that contains m, and F (n,m-1) represents a division that does not contain m.

Code:

  Recursive and recursive code can also be written according to the recursion above.

Recursive code still has a recurring calculation, and time costs a lot.

Recursive code is the process of calculating F (n,n) to F (), so the time and space complexity are O (n^2), and because F (n,m) is and F (n-m,m), F (n,m-1), it is not suitable for space complexity optimization.

Code See GitHub: Integer divide 1

Algorithm notes-integer Division 1

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.