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;
}

猜你喜欢

转载自www.cnblogs.com/buringstraw/p/9990496.html