M apples put on n plates problem

Source: Internet
Author: User

This problem, seems to be a simple permutation combination problem, but with different constraints, will become a different problem, it feels wonderful, summed up the list

Question One

Problem Description: Put m the same apples on n the same plate, allow some tray empty, ask how many different methods? (Note: 5,1,1 and 1,1,5 are the same kind of sub-method)

Problem Solving Analysis:

Set F (m,n) for M apples, N for the number of plates, then the n is discussed first,

    • When n>m: There must be n-m a plate is always empty, remove them on the number of Apple methods to be placed no effect. i.e. if (n>m) f (m,n) = f (m,m)
    • When n <= m: Different methods can be divided into two categories: 0 of the program number, not including 0 of the program number
    1. Contains 0 of the scheme number, that is, at least one plate is empty, which is equivalent to F (m,n) =f (m,n-1);
    2. Does not contain 0 of the scheme number, that is, all the plates have apples, which is equivalent to remove an apple from each plate, does not affect the number of different methods, namely F (m,n) =f (m-n,n). The total number of put apples is equal to the sum of the two, that is F (m,n) =f (m,n-1) +f (m-n,n)

Description of the recursive export conditions:

    • When n=1, all apples must be placed on a plate, so return 1;
    • When the m==0 (no Apple can be placed), defined as 1 kinds of release method;

Using Recursive method

int fun (int m, int n) {//m apples on n plates There are several ways    if (m==0 | | n==1)        return 1;    if (n>m)        return Fun (m,m);    else        return Fun (m,n-1) +fun (m-n,n);}

Using the Dynamic programming method:

                        Int[][] mat=new int[m+1][n+1];for (int i = 0; I <=m; i++) {mat[i][0]=0;mat[i][1]=1;} for (int i = 0; I <=n; i++) {mat[0][i]=1;} for (int i = 1, i <=m; i++) {for (int j = 1; J <=n; J + +) {if (i<j) Mat[i][j]=mat[i][i];elsemat[i][j]=mat[i][j-1]+mat [I-j] [j];}} return mat[m][n];        

Question Two

Problem Description: The sum of integer n divided into k integers and each number greater than or equal to a less than or equal to B, the number of different methods

int Dynamics (int n, int k, int min) {//n is divided into k integers, the smallest is greater than or equal to Min, the largest is no more than B    if (n < min) return 0;  When the remainder is smaller than min, it does not meet the requirements, return 0    if (k = = 1) return 1;    int sum = 0;    for (int t = min; t <= B; t++)    {        sum + = Dynamics (n-t, k-1, T);    }    return sum;}

Question Three

M----> Same, n----> Same, cannot be empty. How many ways to put m apples into n plates? Note, for example, that the two scenarios, such as 1, 2, and 2 and 1, are a scenario.

Idea, first put each plate of an apple, so the problem is converted to: M-n an apple into the N plate, the plate allows empty, that is, problem 1

M apples put on n plates 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.