Returns the split score of an integer ..
One solution is the primary function.
# Include <iostream> # include <stdio. h> # include <string. h> # include <algorithm> # include <string> # include <ctype. h> using namespace std; # define MAXN extends int dp [2] [130]; int main () {int n; while (scanf ("% d", & n )! = EOF) {memset (dp, 0, sizeof (dp); dp [0] [0] = 1; for (int I = 1; I <= n; I ++) {for (int j = 0; j <= n; j ++) {for (int k = 0; j + k * I <= n; k ++) {dp [I % 2] [j + k * I] + = dp [(i-1) % 2] [j];} dp [(i-1) % 2] [j] = 0 ;}} printf ("% d \ n", dp [n % 2] [n]);} return 0 ;}
View Code
There is also a dp method
Dp [n] [m] indicates the number of solutions to split n into numbers not greater than m.
During the transfer, the data is classified by the maximum number in the split ..
Dp [n] [m] = sum {I = 1 ~ N} (dp [I] [n-I]) ---- the maximum number of classes is 1 ~ N
The code is written as recursive memory-based search.
# Include <stdio. h> # include <string. h> # include <algorithm> # include <string> # include <ctype. h> using namespace std; # define MAXN short int dp [130] [130]; int dfs (int n, int m) {if (dp [n] [m]! =-1) return dp [n] [m]; if (m = 0) return dp [n] [0] = 0; if (n = 0) return dp [0] [m] = 1; if (m> n) return dfs (n, n); return dp [n] [m] = dfs (n M-1) + dfs (n-m, m);} int main () {memset (dp,-1, sizeof (dp); int t, x; while (scanf ("% d", & x )! = EOF) {printf ("% d \ n", dfs (x, x);} return 0 ;}
View Code
Hdu1028: integer splitting