POJ-----1664---放苹果---递归 思维
2017-03-25 19:02
253 查看
放苹果
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
Sample Output
相当于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;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 33440 | Accepted: 20735 |
把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;
}
相关文章推荐
- POJ:1664 放苹果(递归,思维)
- POJ-1664 放苹果(递归的运用)
- POJ1664 放苹果(递归)
- POJ 题目1664 放苹果(递归)
- 经典递归问题--放苹果POJ【1664】
- poj--1664--放苹果(递归好体)
- poj 1664 放苹果(递归)
- poj 1664 放苹果(递归)
- poj--1664--放苹果(递归好体)
- 递归及递推问题系列之 放苹果 poj 1664
- POJ 1664 放苹果(递归)
- POJ 1664 放苹果【DFS/递归】
- POJ 1664 分苹果(递归)。
- poj 1664 放苹果 递归
- POJ 1664放苹果(递归问题)
- POJ 1664 放苹果(递归或DP)
- poj1664递归放苹果
- POJ 1664 /NYOJ 758 放苹果问题(递归)
- POJ 1664 放苹果(递归或DP)
- POJ 1664 求m个苹果放入n个盘子的不同放法数目 递归 分类讨论