计蒜客 划分整数
$dp(i,j,lim),(j \le i)$表示将$i$划分为不超过$j$个正整数且最大正整数不超过$lim$的方案数。
AC代码
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 300 + 5; LL dp[maxn][maxn][maxn]; LL solve(int n, int k, int lim) { if(k*lim < n) return 0; if(n == 0 && k == 0) return 1; if(dp[n][k][lim] != -1) return dp[n][k][lim]; dp[n][k][lim] = 0; for(int i = 1; i <= lim; i++) { dp[n][k][lim] += solve(n-i, min(n-i, k-1), i); } return dp[n][k][lim]; } int main() { int n, k; while(scanf("%d%d", &n, &k) == 2) { memset(dp, -1, sizeof(dp)); printf("%lld\n", solve(n, min(n,k), n)); } return 0; }
如有不当之处欢迎指出!