Algorithm notes-integer Division 2

Source: Internet
Author: User
Tags integer division

Title Source: NYOJ176

Problem Description:

Divide a positive integer m into n positive integers, how many methods are there?

Example: The sum of 5 is divided into 3 positive positive numbers, there are two kinds of methods:

1 1 3

1 2 2

Input:

The first line is an integer t representing a common T-group test data (T<=50)
Each set of test data is a two positive integer m,n, where (1<=n<=m<=100) represents the number of positive integers to split and a positive integer to split.

Output:

Outputs the number of split methods for each group.

Analysis:

  The topic can be replaced by the equivalent description: Put m the same Apple on n the same plate, do not allow some of the plate empty, ask how many different methods, where n plate is exactly equivalent?

Assuming that f (m,n) represents the method of placing m apples in n plates, the following recursive relationship can be obtained:

1, M < N, f (m,n) = 0, because the plate is not allowed to be empty;

2, M = N, f (m,n) = 1, in order to ensure that no plate empty, can only be placed on each plate;

3, M > N, first to each plate inside into 1 apples, and the remaining m-n Apple, the remaining m-n Apple can choose to put in 1, 2 ... n plates, so there are:

F (m,n) = f (m-n,1) + f (m-n,2) +......+f (m-n,n)

4. Initial conditions:

When N=1, f (m,n) = 1, that is, only m apples can be placed on this plate;

Code:

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

Recursive code See GitHub: Integer Division 2

Algorithm notes-integer Division 2

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.