1. 程式人生 > >POJ1664|DFS水題

POJ1664|DFS水題

放蘋果

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 38040 Accepted: 23379

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

#include<bits/stdc++.h>
using namespace std;

int cnt,m,n;

void dfs(int step,int tot,int li){
    if(step>n){
        if(tot==m){
            cnt++;
        }
        return ;
    }
    for(int i=li;i+tot<=m;++i){
        dfs(step+1,tot+i,i);
    }
}

int main(){
    int T;
    cin>>T;
    while(T--){
        cnt=0;
        cin>>m>>n;
        dfs(1,0,0);
        cout<<cnt<<endl;
    }
    return 0;
}