Time
limit:1000MS
Memory Limit:10000KB
64bit IO Format:%i64d &A mp %i64u Submit Status
Description
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
1#include <cstdio>2 using namespacestd;3 intFintMintN)4 {5 if(n==1|| m==0)6 return 1;7 if(n>m)8 returnf (m,m);9 ElseTen returnF (m,n-1) +f (M-n,n); One } A intMain () - { - intM,n; the intT; -scanf"%d",&t); - while(t--) - { +scanf"%d%d",&m,&n); -printf"%d\n", F (m,n)); + } A return 0; at}
Recursion.
Set F (m,n) to M apples, n plates of the number of methods, first to n discussion:
When the n>m must have n-m a plate is always empty; when n<=m, 1) at least one plate is empty, which 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, F (m,n) =f (M-n,n);
Recursive exit conditions: When n=1, all apples must be placed on a plate, returning 1; when there is no apple, it is defined as a method of placing;
Two paths of recursion: the first will gradually decrease to n==1, and the second m will gradually decrease, because n>m, RETURNF (m,m), will eventually reach the exit m==0