Rational ball placement in recursion

describe

Put n different balls into m identical boxes. After all the balls are placed, it is required that there is no empty box at the end! Find the total number of different placements.

Enter a line of two numbers n and m
n represents the number of balls, m represents the number of boxes

(0<n≤20) (0≤m≤20) Output different and reasonable total number of methods

sample input

3 2
Sample output
3
prompt recursion

Stirling numbers of the second kind

Problem solving ideas

    Pay attention to the requirements of the observation question, the boxes are the same, the balls are different , so no matter which box the ball is placed in, it is the same , but the difference is whether the ball is placed in the same box as those balls or it is in the same box by itself inside.

    The recursion of this question is relatively simple. First of all, we can think of a total of four cases :

1.m=0;

    When the number of boxes is 0, it is obvious that the placement method is 0 . (How can I put the ball without a box)

2.m=1;

    When the number of boxes is 1, all the balls must be placed in this box, so the method is 1.

3.m=n;

    The problem requires that there can be no empty boxes at the end, so when the number of balls and the number of boxes are equal, each box puts a ball to divide the balls equally, so the method is 1 .

4. m<n; (this time is more complicated, please read below patiently)

    When the number of boxes is less than the number of balls, it means that there must be more than one balls in one or more boxes. Then we need to find the recursion formula. When the number of boxes is m and the number of balls is n-1 , let the placement method at this time be f(n-1)(m) ; well, now add a ball, the number of balls becomes n, then the added ball Where to put it? Think about it, is there a ball in every box when the added ball is not placed ? Is there only two ways to put the added ball :

    ①This ball shares a box with other balls: select one of the boxes ( there are m options ), put the ball in this box, and the other balls are placed in the same box as f(n-1)(m). Is the placement method at this time m* f(n-1)(m) . (I'll still draw a picture, it's easier to understand, see below)


    ②This ball occupies a box: first select a box, vacate the ball in this box (move it to other boxes), and then put the added ball in this empty box (this box has been determined to put a ball), At this time, should the other n-1 balls be placed in the remaining m-1 boxes ? Is the placement method at this time f(n-.1)(m-1).

5. m>n;
    When the number of boxes is greater than the number of balls, even if there is only one ball in a box, it will not be enough, and there will always be empty boxes in the end, so the method is 0 at this time .
Reference procedure
#include<iostream>
using namespace std;
int m,n,i,j;
long long s[25][25];//Define as long long, otherwise it will explode
intmain()
{
	cin>>n>>m;
	for(i=1;i<=n;i++)
		for(j=0;j<=m;j++)
            if(j==0)s[i][j]=0;//The number of boxes is 0
            else if(j==1)s[i][j]=1;//The number of boxes is 1
                else if(i==j)s[i][j]=1;//The number of boxes is equal to the number of balls
                    else if(i>j)s[i][j]=j*s[i-1][j]+s[i-1][j-1];//The number of boxes is less than the number of balls
                        else if(i<j)s[i][j]=0;//The number of boxes is greater than the number of balls
	cout<<s[n][m];
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324443754&siteId=291194637