题目链接:点击打开链接
问题 D: Divide the pears
时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 3 状态
题目描述
Macro非常喜欢吃梨,有一天他得到了ACMICPC组委会送给他的一筐梨子。他比较心疼学生,就打算把梨子分给学生吃。现在他要把M个梨子放到N个盘子里面 (我们允许有的盘子为空) ,你能告诉Macro有多少种分法吗?
(请注意,如果有三个盘子,我们将5,1,1和1,1,5,视为同一种分法)
输入
第一行是一个整数t,代表有t组样例。
第二行有两个整数M 和 N 代表有M个梨和N个盘子。
输出
输出有多少种方法
样例输入
1
7 3
样例输出
8
参考:点击打开链接
数据应该不大,递归就可以,如果卡时间的话,可以考虑打表。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define LL long long
#define CLR(a,b) memset(a,b,sizeof(a))
int f(int m,int n)
{
if (n == 1)
return 1;
if (m == 0)
return 1;
if (m < n)
return f(m,m);
return f(m-n,n) + f(m,n-1); //盘子不空 + 至少空一个
}
int main()
{
int u;
scanf ("%d",&u);
while (u--)
{
int m,n; //m为梨数,n为盘子数
scanf ("%d %d",&m,&n);
printf ("%d\n",f(m,n));
}
return 0;
}