您的位置:首页 > 其它

POJ-----1664---放苹果---递归 思维

2017-03-25 19:02 253 查看
放苹果

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 33440 Accepted: 20735
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

相当于m个数拆分成n部分

把n个盘子分为两部分,一部分存在有点盘子中没有苹果,即有0存在,另一部分相当于没有0

即f[m]
= f[m][n-1] + f[m-n]

f[m][n-1]相当于m个苹果放在n-1个盘子中的所有情况再加一个空盘子,f[m-n]
相当于n个盘子中都有1个苹果,剩下m-n个再分放在n个盘子中

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<iostream>
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5+10;
typedef long long LL;
using namespace std;
int f(int m, int n){
if(m < 0) return 0;
if(m == 0 || n == 1) return 1;
return f(m, n-1) + f(m-n, n);
}
int main(){
int t;
cin >> t;
while(t--){
int m, n;
cin >> m >> n;
cout << f(m, n) << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: