POJ 1664 放苹果
来源:互联网 发布:npm run windows 编辑:程序博客网 时间:2024/05/24 07:21
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
17 3
Sample Output
8
此问题可分为两个子问题:当m<n时,如3个苹果放入4个盘子中,和3个苹果放3个盘子的方法数是一样的。所以m<n时,f[m][n]=f[m][m];
当m>=n时,可分为两种放法,
1. 至少有一个盘子为空,则有f[m][n-1]种放法。
2. n个盘子都不为空,则先将n个苹果一个一个放入n的盘子中,剩下的m-n个苹果再放入n个盘子中。此放法就相当于m-n个苹果放n个盘子里面 ,有f[m-n][n]种放法。
所以状态转移方程为f[i][j]: if(i>=j)f[i][j]=f[i][j-1]+f[i-j][j]
if(i<j) f[i][j]=f[i][i];
结束条件:当n=1时,所有苹果都必须放在一个盘子里,所以返回1 。 当没有苹果可放时,定义为1种放法#include <iostream>using namespace std;int fun(int m,int n){if(m==0||n==1) return 1;if(m<n) return fun(m,m);if(m>=n) return fun(m,n-1)+fun(m-n,n);}int main(){int t;cin>>t;while(t--){int m,n;cin>>m>>n;int ans=fun(m,n);cout<<ans<<endl;}}
0 0
- poj 1664 放苹果
- poj 1664 放苹果
- poj 放苹果 1664
- POJ 1664 放苹果
- POJ 1664 放苹果
- POJ 1664 放苹果
- POJ-1664-放苹果
- poj - 1664 - 放苹果
- poj 1664 放苹果
- POJ-1664(放苹果)
- poj 1664 放苹果
- POJ 1664 放苹果
- POJ 1664 放苹果
- POJ 1664 放苹果
- POJ 1664 放苹果
- poj 1664 放苹果
- poj 1664 放苹果
- POJ 1664 放苹果
- 面向对象程序的设计过程
- block和inline的区别
- TCP/IP 网络编程 (三)
- leetcode-Longest Substring Without Repeating Characters
- CentOS6系统上Yii/Yii2/Nginx/PHP-FPM配置实例
- POJ 1664 放苹果
- struts2请求基本简要流程
- PAT1071(字符串)
- 结构体和类的区别
- <Context path="/struts2" docBase="D:\strustworkspace\struts2\WebRoot" reloadable="true"/>配置问题
- 12.如何自学Struts2发送邮件和验证补充[视频]
- 迭代器和算法
- static/static const 的初始化
- WCF服务创建实例说明