Programming Algorithm-division code (C)

Source: Internet
Author: User

Programming Algorithm-division code (C)
Division code (C)

 

 

 

Question: There are n items that have no difference. divide them into groups no more than m, and find the remainder of the division method D. M.

For example, m = 3 of n = 4, result = 4 (, 2; 4)

 

UseDynamic Planning (DP)Method,

M of n is divided into a. If each I has,The set of {A-1} is m division of n-m;When a = 0, it is the m-1 division of n..

Recursive Formula: dp [I] [j] = dp [I] [j-I] + dp [I-1] [j]

 

Code:

 

/* * main.cpp * *  Created on: 2014.7.20 *      Author: spike *//*eclipse cdt, gcc 4.8.1*/#include 
 
  #include 
  
   class Program {static const int MAX_N = 100;int n=4, m=3;int M=10000;int dp[MAX_N+1][MAX_N+1];public:void solve() {dp[0][0] = 1;for (int i=1; i<=m; ++i) {for (int j=0; j<=n; ++j) {if (j-i>=0) {dp[i][j]=(dp[i-1][j]+dp[i][j-i])%M;} else {dp[i][j]=dp[i-1][j];}}}printf(result = %d, dp[m][n]);}};int main(void){Program iP;iP.solve();return 0;}
  
 

Output:

 

 

result = 4


 

 

 

 

 

Related Article

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.