Question Portal http://poj.org/problem? Id = 1664
Set $ dp [I] [j] $ to the total number of $ I $ apples on $ j $ plates.
$1. $ When the number of apples is less than the number of plates $ (M <N) $, the remaining $ N-M $ plates are empty, and the problem is equivalent to placing an apple in $ M $ plates: $ dp [M] [N] = dp [M] [M] $
$2. $ When the number of apples is greater than or equal to the number of dishes $ (M \ geq N) $, it can be broken down into at least one Apple for all dishes $ (dp [M-N] [N]) $ and at least one plate is empty $ (dp [M] [N-1]) $ merge of the two subresults: $ dp [M] [N] = dp [M-N] [N] + dp [M] [N-1] $
$3. $ recursive exit:
$ Dp [M] [1] = 1; \ dp [0] [N] = 1 $
Note: Why not use $ M = 1 $? There is only one fruit left. However, for example, if $ dp [3] [3] = dp [3] [2] + dp [0] [3] $, $ M $ is $0 $, data is not found at exit $ M = 1 $.
/******************************** Author: jusonalienEmail: [email protected] school: South China Normal UniversityOrigin: POJ *********************************/# include <iostream> # include <cstdio> # include <map> # include <cstring> # include <string> # include <set> # include <queue> using namespace std; int dp (int m, int n) {// recursive version if (m = 0 | n = 1) return 1; if (m <n) return dp (m, m); else return (dp (m-n, n) + dp (m, n-1);} int DP [15] [15]; void solve () {// recursive version for (int I = 0; I <= 12; ++ I) DP [0] [I] = 1; for (int I = 0; I <= 12; ++ I) DP [I] [1] = 1; for (int I = 1; I <= 10; ++ I) {for (int j = 1; j <= 10; ++ j) {if (I <j) DP [I] [j] = DP [I] [I]; else DP [I] [j] = DP [I-j] [j] + DP [I] [j-1] ;}} int main () {int m, n, t; solve (); scanf ("% d", & t); while (t --) {scanf ("% d", & m, & n ); printf ("% d \ n", DP [m] [n]); // printf ("% d \ n", dp (m, n ));} return 0 ;}
POJ1664 count DP