Dynamic Programming Learning Series-dividing DP (ii)

Source: Internet
Author: User

Division DP The second problem, Wikioi 1039, and the first question product The biggest idea is different.

Title requirements:
Divide an integer n into the K section and ask how many different divisions there are.
true and thought :
In fact, with some of the primary school Olympic Games a bit like, we first look at an example:
7 divided into 3 parts, there are four ways:
1,1,5;1,2,4;1,3,3;2,2,3.
How do we think when we think about things in our brains? Obviously, starting from 1, 1 is the 1th part, and then the remaining 6 is divided into 2 parts, we are still starting from 1, 1 is the 2nd part, then 5 is the 3rd part, also next consider the 2nd part is 2, then the 3rd part is 4, then consider the 2nd part is 3, the 3rd part is 3 Because each part can not be smaller than the previous part (avoid duplication), so 1 for the 1th part of the consideration, and then consider the 1th part is 2, similarly, from the 2nd part is 2, so only 2, 3 this situation.
The mind of the brain is a bit like a search, which is why I like the deep search algorithm.
However, now we do not want to use search to do, but with dynamic planning to do, we should think about it.
A famous quote from Daniel: Dynamic Programming = = Memory Search
We also know that dynamic planning is to use the last state to launch the current state, then we can consider: the 1th part is taken out separately, considering all the circumstances of the 1th part and all the corresponding elimination of the first part into the m-1 part of the case, so, there is a state transfer equation :
DP[I][J] =∑dp[i-k][j-1] (1 <= k <= i/j)
You can also simplify, because Dp[i-1][j-1] =∑dp[i-k-1][j-1]:
Dp[i][j] = dp[i-1][j-1] + dp[i-j][j]
This formula can be understood as: first consider the 1th part is 1, the rest of the 1th part must be greater than 1, so each part of the minus 1 is no harm, this value is also we know.

Code:

#include <bits/stdc++.h>

using namespace std;

int n,m,dp[205][10];

int main ()
{
    scanf ("%d%d", &n,&m);
    Dp[0][0]=1;
    for (int i=1;i<=n;i++)
    {for
        (int j=1;j<=m;j++)
        {
            if (j<=i)
                dp[i][j]=dp[i-j][j]+dp[ I-1][J-1];
        }
    }
    printf ("%d\n", Dp[n][m]);

    return 0;
}

Summary:
1, although programming to stand in the angle of the machine, but occasionally run with the human brain is also good;
2, dynamic planning is the memory of the deep search;
3, remember not to think about repetition.

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.