Problem of putting apples (combinatorial math Classics)

Source: Internet
Author: User

Turn from: 52761238

"Problem 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.

"Problem Analysis"

The problem with apples is very complicated at first glance, the plates are the same, and the apples are the same; as long as there are as many apples in each plate, no matter how the order is ultimately obtained is the same method. I belong to the beginner algorithm, for the algorithm is not familiar with the problem will be the person's thinking to think about the problem, I will think of empty a plate is what situation, empty two plate is what situation, a plate is not empty is what situation. The more you think about the confusion, the end is not to solve the problem, but the current look at the recursive algorithm. It seems that because I think more, in fact we need to simplify the problem. In terms of the problem of putting apples on the plate, we only need to have two situations: empty plates and empty plates.

1. Free PLATE: F (m,n) =f (m,n-1)//Empty plate Many people will have doubts, this is not only a case of empty plates? What about the 2 3 empty plates? This requires the idea of recursion, with a step by turn N into n-1 you will find that it is 2, 3 empty plate situation.

2. No empty plates:

< Span style= "color: #333333; Font-family: ' Times New Roman ', times, serif; line-height:26px; " > so: f (m-n,n)       m>=n                  

< Span style= "color: #333333; Font-family: ' Times New Roman ', times, serif; line-height:26px; " >

Therefore:f (m,n) =f (m,m) m<n

Write here the main expression is basically finished, but recursion needs to have an end condition, the end condition is not difficult to find, when there is only one plate obviously only one way, and there is no Apple and only one apple when there is only one method. i.e. f (m,n) =1 n=1,m=0

Comprehensive:

F (m,n) =1 n=1,m=0

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

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

#include <iostream>#include<cstring>using namespacestd;Const intmaxm=10000;intM[MAXM],N[MAXM],K[MAXM];intPutapple (intMintn);intMain () {memset (k,0,sizeof(k)); intT; CIN>>T;  for(intI=1; i<=t;i++) {cin>>m[i]>>N[i]; }     for(intI=1; i<=t;i++) {K[i]=putapple (M[i],n[i]); cout<<k[i]<<Endl; }}  intPutapple (intMintN) {    if(m==0|| n==1)return 1; if(n>m)returnputapple (m,m); Else        returnPutapple (m,n-1) +putapple (M-n,n);}

Problem of putting apples (combinatorial math Classics)

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.