Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 33671 | Accepted: 20830 |
Description
Input
Output
Sample Input
1 7 3
Sample Output
8
代码实现
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 int coun(int m, int n); 5 int main(){ 6 int x, m, n; 7 while(cin >> x){ 8 while (x--){ 9 cin >> m >> n; 10 int res = coun(m, n); 11 cout << res << endl; 12 } 13 } 14 return 0; 15 } 16 int coun(int m, int n){ 17 if (m == 0 || n == 1) 18 return 1; 19 if (n > m) 20 return coun(m, m); 21 else 22 return coun(m, n - 1) + coun(m - n, n); 23 }
运行结果
1 7 3 8 3 2 5 2 5 2 3 8 5 18
算法分析
典型的用动态递归解法,其实这根将一个整数m分成n个整数之和是类似的。设f[m][n]为将m分成最多n份的方案数,且其中的方案不重复,即每个方案前一个份的值一定不会比后面的大。则有:f[m][n] = f[m][n - 1] + f[m - n][n],其中f[m][1]=1、f[0][n]=1.其中的含义是:分的方案包含两种,一是方案中不含有空的情况,则从每个盘子里面拿出一个来也不影响最终的结果,即f[m][n] = f[m - n][n],二是方案中含有的空的情况,至于含有几个空暂时不考虑,只用考虑一个空的就行,即f[m][n] = f[m][n - 1],在这种递归调用中会重复调用含有空的情况,也就是最终包含了各种可能为空的情况。
原文:http://www.cnblogs.com/2016024291-/p/6686261.html