poj-1664.放苹果.(递推)

放苹果

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 40548   Accepted: 24737

Description

把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。

Input

第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。

Output

对输入的每组数据M和N,用一行输出相应的K。

Sample Input

1
7 3

Sample Output

8

Source

 1 /*************************************************************************
 2     > File Name: poj-1664.放苹果.cpp
 3     > Author: CruelKing
 4     > Mail: [email protected] 
 5     > Created Time: 2019年09月09日 星期一 20时14分27秒
 6     我们用dp[i][j]表示把i颗苹果放到j个盘子中
 7     本题思路:当有m颗苹果要放到n个盘子上时,如果m < n,那么说明必定有n - m个盘子要空着,所以方法数等效于dp[m][m],加入此时至少有一个盘子不放苹果,那么就相当于把m个苹果放到n - 1个盘子里,假如此时所有盘子里都放有苹果,那么我们可以把每个盘子里取出一颗苹果,也就是说dp[m][n]等效于dp[m - n][n];当盘子只有一个或者没有苹果可以放时我们返回1表示有一种放法.
 8  ************************************************************************/
 9 
10 #include <cstdio>
11 using namespace std;
12 
13 int m, n;
14 
15 int dfs(int a, int b) {
16     if(b == 1 || a == 0) return 1;
17     if(a < b) return dfs(a, a);
18     else return dfs(a, b - 1) + dfs(a - b, b);
19 }
20 
21 int main() {
22     int t;
23     scanf("%d", &t);
24     while(t --) {
25         scanf("%d %d", &m, &n);
26         printf("%d\n", dfs(m, n));
27     }
28     return 0;
29 }

猜你喜欢

转载自www.cnblogs.com/bianjunting/p/11494177.html