BuringStraw

BuringStraw

POJ1664|DFS Water Problem

Put Apples#

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

Description#

Put M identical apples in N identical plates, allowing some plates to be empty, how many different ways are there to distribute them? (denoted by K) 5, 1, 1 and 1, 5, 1 are considered the same distribution.

Input#

The first line is the number of test cases t (0 <= t <= 20). Each of the following lines contains two integers M and N, separated by a space. 1<=M, N<=10.

Output#

For each input of M and N, output the corresponding K in one line.

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;
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.