Put m the same apples on n the same plate, allow some plates to be empty, ask how many different ways? (denoted by k) 5,1,1 and 1,5,1 are the same kind of sub-method.
Input
The first line is the number of test data t (0 <= T <= 20). Each of the following lines contains two integers m and n, separated by a space. 1<=m,n<=10.
Output
For each set of data entered M and N, the corresponding k is output in one line.
Sample Input
17 3
Sample Output
8
Analysis:
Problem Solving Analysis:
set F (m,n) for M apples, N for the number of plates, then the n is discussed first,
when n>m: there must be n-m a plate is always empty, remove them on the number of Apple methods to be placed no effect. i.e. if (n>m) f (m,n) = f (m,m)
when n<=m: Different methods of release can be divided into two categories:
1, there is at least one plate empty, that is equivalent to f (m,n) = f (m,n-1);
2. All plates have apples, which is equivalent to taking an apple from each plate, without affecting the number of different methods, i.e. f (m,n) = f (m-n,n).
The total number of put apples is equal to the sum of the two, that is F (m,n) =f (m,n-1) +f (m-n,n)
description of the recursive export conditions:
when n=1, all apples must be placed on a plate, so return 1;
When there is no apple can be put, defined as 1 kinds of release method;
two routes of recursion, the first n will gradually reduce, will eventually reach the exit N==1;
the second m will gradually decrease, because when n>m, we will return F (m,m) so we will eventually reach the exit m==0.
Code
#include <iostream>#include<stdio.h>#include<algorithm>#include<math.h>using namespaceStd;typedefLong LongLL;#defineMax_v 10005LL F (intMintN) { if(m==0|| n==1) return 1; if(n>m)returnf (m,m); Else returnF (m,n-1) +f (M-n,n);}intMain () {intT; scanf ("%d",&t); intn,m; while(t--) {scanf ("%d%d",&m,&N); printf ("%i64d\n", F (m,n)); } return 0;}
POJ 1644-point Apple (recursive solution)