Topic links
Dynamic programming recursion for classical DP integer partitioning problem
Set DP[I][J] = Split fraction I, the largest so the number does not exceed the number of J scheme.
The first is that the last number does not exceed j-1, at which point the number of scenarios is dp[i][j-1], or the last number is just J, at which point the number of scenarios is dp[i-j][j].
Pay attention to some boundary conditions.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <string>5#include <algorithm>6 using namespacestd;7 #defineMAXN 1308 intdp[ the][ the];9 voidinit () {TenMemset (DP,0,sizeof(DP)); One for(inti =0; I <= the; i++){ Adp[0][i] =1;//0 -dp[i][1] =1;//are split into 1 -dp[1][i] =1; the } - for(inti =2; I <= the; i++){ - for(intj =2; J <= the; J + +){ - if(j<=i) Dp[i][j] = dp[i][j-1] + dp[i-J] [j]; + ElseDP[I][J] =Dp[i][i]; - } + } A } at intMain () { - intN; - init (); - while(SCANF ("%d", &n)! =EOF) { -cout<<dp[n][n]<<Endl; - } in return 0; -}
HDU 1028 Ignatius and the Princess III (integer division)