Add the non-negative integers of k not more than N, so that they are N and a total of how many methods.
Set D (I, j) to indicate the sum of J of a nonnegative integer not exceeding I is the number of methods for I.
D (i, j) = sum{D (k, j-1) | 0≤k≤i}, can be understood as the sum of the previous j-1 number is i-k, the last number is K
There is also a faster recursive approach that transforms the problem into a number of methods that put n balls into K-boxes, which can be empty.
It is equivalent to the number of nonnegative integer solutions of x1 + x2 +...+ xK = N, according to the knowledge of combinatorial mathematics it is easy to calculate the result as C (N+k-1, K-1).
So it can also be recursive: D (i, j) = d (I-1, J) + D (i, j-1)
1#include <cstdio>2 3 Const intM =1000000;4 Const intMAXN = -;5 intD[MAXN +Ten][MAXN +Ten];6 7 intMain ()8 {9 for(inti =1; I <= MAXN; i++)Tend[0][i] =1; One for(inti =1; I <= MAXN; i++) A for(intj =1; J <= Maxn; J + +) -D[I][J] = (d[i-1][J] + d[i][j-1]) %M; - the intN, K; - while(SCANF ("%d%d", &n, &k) = =2&& n && k) printf ("%d\n", D[n][k]); - - return 0; +}code June
UVa 10943 (Math recursion) How does you add?