(递归)poj—1664-放苹果

放苹果

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37404   Accepted: 23033

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

lwx@POJ

扫描二维码关注公众号,回复: 3736562 查看本文章

[Submit]   [Go Back]   [Status]   [Discuss]

题解:m个苹果,n个篮子;难点在于 m>=n;题目中说可以不放有空的,那么我们可以分类讨论,可以分为:

            1> .将 m个苹果放到 n-1 个篮子里,这样满足可以为空的情况,意思是:至少有一个篮子是空的;

             2> .将 m-n 个苹果放到 n 个篮子里,这样满足篮子全满的的情况(n个篮子至少有一个),剩下的m-n个随机放。

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#include<set>
#include<map>
#include<math.h>
#include<vector>
#include<bitset>
#include<iostream>
#define ullmax 1844674407370955161
#define llmax 9223372036854775807
#define intmax 2147483647
#define re register
using namespace std;
int dfs(int m,int n){
    if(n<=1||m<=1)
        return 1;
    else if(n>m)
        return dfs(m,m);
    else
        return dfs(m-n,n)+dfs(m,n-1);
}
int main(){
    int t,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&m,&n);
        printf("%d\n",dfs(m,n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/81949724