Division of dp-number of dividing type

Source: Internet
Author: User

Title Description Description

The integer n is divided into k parts, and each part cannot be empty, and any two partitioning schemes cannot be the same (regardless of order).
For example: N=7,k=3, the following three partitioning schemes are considered to be the same.
1 1 5

1 5 1

5 1 1
Ask how many different sub-methods there are.

Enter a description Input Description

Input: N,k (6<n<=200,2<=k<=6)

Output description Output Description


Output: An integer, which is a different method of division.

Sample input Sample Input

3 ·

Sample output Sample Output

4

Analysis: This problem is actually a classical problem in combinatorial mathematics, which is somewhat similar to the second type of Stirling number. You can think of a number of n as n a small ball, divided by the number of K as a K-box, then the requirements of the subject is:
Put n balls into K-boxes, there is no difference between the ball and the box, and the final result does not allow empty box
The derivation process is similar to the recursive formula for the second class of Stirling numbers:
Total number of cases where n balls are placed in K-boxes = 1, at least one box with only one ball case number + 2, no box there's only one ball .The number of cases

The reason for this partitioning is that this classification is special enough, and 1 and 2 have expressions that can be written out:
1. Since the box is not differentiated, 1 of the cases are the same as the number of cases in which the n-1 a small ball into a k-1 box.
2. No box has only a small ball, then take out each box a small ball, corresponding to "put (n-k) a small ball in the K box of the number of cases"
As to why two equivalence relationships in 1 and 2 are established, they can be proved by assembling A= set B.

Finally, the above narrative is transformed into a DP expression:
F[n][k] represents the case where n balls are placed in a K-box and there is no empty box, then f[n][k] = f[n-1][k-1] + f[n-k][k]
(copy of the Arrogant God)

Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace Std;
int dp[1000][1000];
int main ()
{
int i,j,x,n,k;
cin>>n>>k;
for (i=1;i<=n;i++)
for (j=1;j<=i;j++)
{
if (i==j) dp[i][j]=1;
else dp[i][j]=dp[i-j][j]+dp[i-1][j-1];
}
cout<<dp[n][k]<<endl;
return 0;
}

Division of dp-number of dividing type

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.