Calculate the number of divisions of an integer

Problem Description:

Problem Description: Divide a positive integer into the sum of a series of positive integers.
N=n1+n2+…+nk(n1>=n2>=n3….>=nk)
is called a division of positive integer n, and there are different divisions of a positive integer. For example 6
6=6

6=5+1

6=4+2 6=4+1+1

6=3+3 6=3+2+1 6=3+1+1+1

6=2+2+2 6=2+2+1+1 6=2+1+1+1+1

6=1+1+1+1+1+1

Analysis of Algorithms:

If P(n, m) is set to represent all the different divisions of the positive integer n, the maximum addend is not greater than the number of divisions of m.
Then you can create a recursion:

(1)、P(n,1)=1,n>=1;

(2) P(n,m)=p(n,n),m>=n;

(3) P(n,n)=1+P(n,n-1);

(4)P(n,m)=P(n,m-1) + P(n-m,m), n>m>1

The above three can be understood by everyone, but only the fourth one is half-understood. Explain the analysis of the fourth one:
"The maximum addend of a positive integer n is not greater than the division number of m" can be understood as "n The maximum addend of is not greater than the sum of the division number P(n, m-1) of m-1" and the sum of "the maximum addend of n is the division number of m".

For example : P(6,4) is equal to the sum of "the division number P(6,3) whose maximum addend of 6 is not greater than 3" and "the number of divisions whose maximum addend is 4". And "the number of divisions where the maximum addend of 6 is 4" is 2. It can also be seen from the above example that "the number of divisions where the maximum addend of 6-4 is not greater than 4" is 2, which is P( 6-4,4)=P(2,4)=2.

In simple terms, the number of divisions where the maximum addend of n is m is not P(nm, nm). It can be understood in this way, but this is only a special case and cannot represent all of them. For example, the number of divisions with the maximum addend of 2 can be to try it. Surprisingly, it turns out that it is not.
According to the above analysis, the recursive function formula can be obtained:
write picture description here

#include<stdio.h>

int P(int n,int m){
    if(m==1 || n==1) return 1;
    if(m>n) return P(n,n);
    if(m==n) return 1+P(n,n-1);
    return P(n,m-1)+P(n-m,m);
} 
int main(){
    int n,s;
    printf("Please input a integer for getting the number of division\n");
    scanf("%d",&n);
    s = P(n,n);
    printf("The number is division of %d is %d\n",n,s);
    return 0;
}

Guess you like

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