Solution of Integer partition Problem 2-dynamic programming

Source: Internet
Author: User
Tags integer division

Integer division---a question for a long conversation:
1 Practice combined Mathematics ability.
2 Practice Recursive thinking
3) Practice DP
In short, a classic can not be a classic title:
This good question asks:
1. divides n into the sum of the number of positive integers.
2. Divides n into the sum of k positive integers.
3. Divides n into the maximum number of no more than K division number.
4. Divides n into the sum of several odd positive integers.
5. Dividing n into the sum of a number of different integers.

1. Dividing n into less than m:

1. If you divide multiple integers, you can have the same:

dp[n][m]= dp[n][m-1]+ Dp[n-m][m] Dp[n][m] represents the division of an integer n, each number not greater than the number of M.
The dividing number can be divided into two kinds of cases:
A. Each number in the division is less than M, the equivalent of each number is not greater than m-1, so the number is divided into dp[n][m-1].
B. There is a number in the division of M. that is minus m in N, and the rest is equivalent to dividing the n-m, so dividing the number into dp[n-m][m];

2. If a number of different integers are divided:

dp[n][m]= dp[n][m-1]+ Dp[n-m][m-1] Dp[n][m] represents the division of an integer n, each number not greater than the number of M.
The same division of circumstances is divided into two categories:
A. Each number in the division is less than m, equivalent to each number not greater than m-1, divided into dp[n][m-1].
B. There is a number of M in the division, minus M in N, leaving quite a n-m division,

And each number is not greater than m-1, so the number is divided into dp[n-m][m-1]


2. Dividing n into K numbers:

dp[n][k]= dp[n-k][k]+ Dp[n-1][k-1];

Methods can be divided into two categories:
The first Category: N parts does not contain 1 of the method, in order to ensure that each is >= 2, you can first take out K 1 points
To each, and then the remaining n-k into a K, divided into the law: dp[n-k][k]
The second Category: N part of at least one copy of 1, you can first that out of a 1 as a separate 1, left
Under the n-1 and then divided into k-1, divided into the law are: dp[n-1][k-1]

   

3. Divide n into several odd-numbered divisions:

G[I][J]: Divide i into J-even

F[I][J]: Divide i into J-odd
G[I][J] = F[i-j][j];
F[I][J] = F[i-1][j-1] + g[i-j][j];

Methods can be divided into two categories:

The first class: I take out J 1 to each part, divide the remaining i-j into J-Odd

The second category: one contains odd 1, the remaining i-1 is divided into j-1 odd, the other one is at least greater than 1, the J 1 is divided into each part, the remaining i-j is divided into J


The code looks like this (reprint):

* * * * * * hit1402.c * * Created on:2011-10-11 * Author:bjfuwangzhu/#include <stdio.h> #include <string. h> #define NMAX Wuyi int Num[nmax][nmax]; Dividing I into a number of int Num1[nmax][nmax] which is not more than J; Divide I into different numbers of int Num2[nmax][nmax] that are not more than J; Dividing I into J number int F[nmax][nmax]; Dividing I into J odd int G[nmax][nmax];
    Divide I into J even void Init () {int i, J;
                for (i = 0; i < Nmax i++) {num[i][0] = 0, Num[0][i] = 0, num1[i][0] = 0, Num1[0][i] = 0, num2[i][0] =
    0, Num2[0][i] = 0;
                for (i = 1; i < Nmax. i++) {for (j = 1; j < Nmax; J +) {if (I < j) {
                NUM[I][J] = Num[i][i];
                NUM1[I][J] = Num1[i][i];
            NUM2[I][J] = 0;
                else if (i = = j) {Num[i][j] = num[i][j-1] + 1;
                NUM1[I][J] = num1[i][j-1] + 1;

            NUM2[I][J] = 1;
                else {Num[i][j] = Num[i][j-1] + num[i-j][j]; NUM1[I][J] = num1[i][j- 1] + num1[i-j][j-1];
            NUM2[I][J] = Num2[i-1][j-1] + num2[i-j][j];
    }} F[0][0] = 1, g[0][0] = 1;
            for (i = 1; i < Nmax. i++) {for (j = 1; J <= I; j) {g[i][j] = f[i-j][j];
        F[I][J] = F[i-1][j-1] + g[i-j][j]; int main () {#ifndef Online_judge freopen ("data.in", "R", stdin); #endif int n, k, I, Res0, Res1, Res2,
    Res3, Res4;
    Init ();
        while (~SCANF ("%d%d", &n, &k)) {Res0 = Num[n][n];
        Res1 = Num2[n][k];
        Res2 = Num[n][k];
        for (i = 0, res3 = 0; I <= N; i++) {Res3 + = F[n][i];
        } res4 = Num1[n][n];
    printf ("%d\n%d\n%d\n%d\n%d\n\n", Res0, Res1, Res2, Res3, res4);
return 0; }

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.