HDU acm1028 Integer Division recursion problem (recursive)

Source: Internet
Author: User
Tags integer division

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&LT;&LT;DP (n,n) <<Endl; A     } the     return 0; +}

HDU acm1028 Integer Division recursion problem (recursive)

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.