poj1664 放蘋果

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

Tips

思路:
遞歸。

條件:m:apple, n:plate
問題:func(m, n)
劃分問題爲若干個子問題:
(1). m < n: func(m, n) = func(m, m);
(2). m >= n: func(m, n) = func(m, n-1) + func(m-n, n);
a. 至少有一個盤子是空的;func(m, n-1)
b. 全部盤子都不爲空,即每個盤子至少有一個蘋果; func(m-n, n)

收斂條件:
當n == 1,即只有1個盤子時,只有1种放置方法;

當m == 0,即沒有蘋果可放置時,也是1种放置的方法;


AC Code

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;

int m,n;

long long f(int m,int n){
	if(m==0||n==1) return 1;
	if(m<n) return f(m,m);
	else
		return f(m,n-1)+f(m-n,n);
}


int main(int argc, char** argv) {
	int t;
	cin>>t;
	while(t--){
		cin>>m>>n;
		cout<<f(m,n)<<endl;
	}
	return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章