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