We use recursive + memory to solve the problem of ordinary integer partitioning: Define f (n,m) to divide the integer n into a series of integers, where addend
Maximum no more than M.
Get the following recursive relationship:
When N==1 | | M==1 has only one division, that is, 1 or 1+1+1......+1.
When M>n is obviously equivalent to F (n,n)
When m==n at this time: I consider two cases where addend contains m or not:
1) Division does not contain m, i.e. f (n,m-1)---all m-1
2) division contains M, at this time only one is M
So when m==n, there is f (n,m) =f (n,m-1) +1
When M<n,
1) contains M, {M,X1,X2,X3....XI} is now equivalent to F (n-m,m)
2) does not contain m when obviously F (n,m-1)
So f (n,m) =f (n,m-1) +f (n-m,m)
Using memory technology to optimize complexity:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineMAXN 1306 intNUM[MAXN][MAXN];7 intdpintNintk)8 {9 if(n==1|| k==1)Ten return 1; One if(k>N) A { -k=N; - if(num[n][n]==-1) the returnnum[n][n]=DP (n,n); - Else - returnNum[n][n]; - } + Else if(k==N) - { + if(num[n][k]==-1) A returnNUM[N][K]=DP (n,k-1)+1; at Else - returnNum[n][k]; - } - Else - { - if(num[n][k]==-1) in returnNUM[N][K]=DP (n,k-1) +DP (nk,k); - Else to returnNum[n][k]; + } - } the intMain () * { $ intN;Panax Notoginseng while(cin>>N) - { thememset (num,-1,sizeof(num)); +COUT<<DP (n,n) <<Endl; A } the return 0; +}
HDU acm1028 Integer Division recursion problem (recursive)