Problem of putting apples

Source: Internet
Author: User

Description

Put m the same apples on n the same plate, allow some plates to be empty, ask how many different ways? (denoted by k) 5,1,1 and 1,5,1 are the same kind of sub-method.

Input

The first line is the number of test data t (0 <= T <= 20). Each of the following lines contains two integers m and n, separated by a space. 1<=m,n<=10.

Output

For each set of data entered M and N, the corresponding k is output in one line.

Sample Input

17 3

Sample Output

8

Source

[email protected]Problem solving ideas: I do not understand the combination of mathematics, see others are said to be a combination of mathematical problems, that is to this kind of bar, the idea is to find a recursive relationship, we may wish to make F (m,n) said that M apples to n a plate how many kinds of methods, the following for different situations to be discussed: (1): When the plate number is 1 There is only one way to put all the apples on a plate. (2): When the number of apples is 1, there is only one way to pay attention to the title, there is no order between the plates, so regardless of the apple on which plate, the results are counted as one. (3): When M<n, because at this time can only be placed on the M-plate (one in a), in fact, the equivalent of the M-Apple on the M-plate, that is f (m,m);(4): When the m==n, at this time in two situations discussed, one is a plate put one, just one, The second is that there is at least one plate that does not put the apples. This is the equivalent of F (m,m-1);(5): When M>n, there are two kinds of discussion, one is at least one plate does not put apples, this is the equivalent of F (m,n-1), the second is to take out n apples one plate, put one, Put the remaining m-n apples on n plates, i.e. f (m-n,n); In summary: Get the recursive expression: F (m,n) =1 when M=1 or n=1;f (m,n) =f (m,m) as M<n;f (m,n) =1+f (m,m-1) when m=n;f (m,n) = F (m-n,n) +f (m,n-1); using memory search in the process of recursion can reduce unnecessary time, and don't forget what you've counted.
1#include <iostream>2 using namespacestd;3#include <string>4 5 intFintMintN) {6 if(m==1|| n==1)7 return 1;8 if(m<N)9 returnf (m,m);Ten if(m==n)//can also be combined as if (m>=n) One return 1+f (m,n-1); A if(m>N) - returnF (m-n,n) +f (m,n-1);  - } the  -  - intMain () { -     inta,b,i; +Cin>>i; -      while(i--){ +Cin>>a>>b; ACout<<f (b) <<Endl; at     } -     return 0; -}
View Code

Problem of putting apples

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.