split number code (C)
This address: Http://blog.csdn.net/caroline_wendy
Title: There are n non-differentiated items, dividing them into no more than M group, to find out the remainder of the partition method number modulus M.
For example: m=3 division of N=4, Result=4 (1,1,2; 1, 3; 2, 2; 4)
Using the Dynamic Planning (DP) method,
N m divides a, assuming that each i has a, the set of {A-1} is the M division of N-m ; a=0, 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 <stdio.h> #inclu De <memory.h>class program {static const int max_n = 100;int n=4, m=3;int m=10000;int dp[max_n+1][max_n+1];p ublic:vo ID 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\n", Dp[m][n]);}; int main (void) {program ip;ip.solve (); return 0;}
Output:
result = 4
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Programming Algorithms-Split number code (C)