[Turn] Summary of the problem of permuting and combining "n balls into m boxes m"

  Source: https://blog.csdn.net/qwb492859377/article/details/50654627

  Balls and boxes can be divided into whether they cannot be distinguished, and whether they can be distinguished, and whether they can be divided into empty boxes, so there are a total of 8 cases, we will discuss them one by one.

1. Same ball, different box, no empty box

C(n-1,m-1), n>=m
0, n<m

Use the plug-in method: there are n-1 gaps between n balls, and now they must be divided into m boxes, and there can be no empty boxes, so just select m-1 gaps in n-1 gaps.

2. The ball is the same, the box is different, and empty boxes are allowed

C(n+m-1,m-1)

Let's continue the discussion in the first case. We can first assume that there are 1 balls in m boxes, so to put it bluntly, there are m+n identical balls, and m different boxes are to be placed, There are no empty boxes. That is, the first case

3. Different balls, same box, no empty box

Stirling numbers of the second kind dp[n][m]
dp[n][m]=m*dp[n-1][m]+dp[n-1][m-1],1<=m <n
dp[k][k]=1,k>=0
dp[k][0]=0,k>=1
0,n<m

This case is the Stirling number of the second kind, and let's understand the transition equation.

For the nth ball, if the previous n-1 balls have been placed in m bins, it is OK to put the nth ball in which bin now, so m*dp[n-1][m] ;

If the first n-1 balls have been placed in m-1 boxes, now the nth ball must be opened in a new box to store, so dp[n-1][m-1]

Nothing else can be transferred

4. The balls are different, the boxes are the same, empty boxes are allowed

sigma dp[n][i],0<=i<=m,dp[n][m] is the second kind of Stirling number in case 3

This situation is to enumerate the number of boxes used under the premise of the third situation.

5. Different balls, different boxes, no empty boxes

dp[n][m]*fact[m], dp[n][m] is the second type of Stirling number in case 3, fact[m] is the factorial of m

Because the balls are different, if the boxes obtained by dp[n][m] are the same, as long as the order of the boxes is defined, it is equal to the current answer

6. Different balls, different boxes, allow empty boxes

power(m,n) means m raised to the nth power

Each ball has m choices, so it is equal to m^n

7. The ball is the same, the box is the same, and empty boxes are allowed

dp[n][m]=dp[n][m-1]+dp[n-m][m], n>=m
dp[n][m]=dp[n][m-1], n<m
边界dp[k][1]=1,dp[1][k]=1,dp[0][k]=1

Now there are n balls and m boxes, I can choose to put 1 ball in all boxes, or not choose this operation.

If this operation is selected, then transfer from dp[nm][m]

If this operation is not selected, then transfer from dp[n][m-1]

8. Same ball, same box, no empty box

dp[nm][m], dp is the same as the seventh case, n>=m
0, n<m

Because no empty boxes are required, we first put 1 ball in each box, and then there are nm balls left, and then the answer will come out according to the situation 7

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324648310&siteId=291194637
^M