Poj1664 put apple

Put M identical apples on N identical plates, and allow some plates to be left empty. How many different ways are there in total? (represented by K) 5, 1, 1 and 1, 5, 1 are the same division.
Input
The first row is the number t of test data (0 <= t <= 20). Each of the following lines contains two integers M and N, separated by spaces. 1<=M, N<=10.
Output
For each set of input data M and N, use one line to output the corresponding K.
Sample Input
1
7 3
Sample Output

8


Ideas:
①When m < n, nm disks will be vacated, then a[m][n] = a[m][m].
②When m >= n, it will be divided into two cases:
1. If there is no empty plate, then put 1 plate in each plate, and then put the remaining mn in n plates, that is, a[m][n] = a[mn][n]
2. One plate is empty, and m are placed in n-1 plates, that is, a[m][n] = a[m][n-1].
You may have questions about this situation. Why only one plate is empty? This is to ensure that there are empty plates. If m apples are randomly placed on n-1 plates, there will definitely be other plates that are empty. .
The sum of the two cases is the result. (ai][j] is the number of solutions where i apples are placed in j plates).
Code:
#include <iostream>
using namespace std;
const int AX = 15;
int a[AX][AX];
int main(){
	int T;
	cin >> T;
	int m , n;
	for( int i = 1 ; i <= 10 ; i++ ){
		a[i][1] = 1;
		a[1][i] = 1;
		a[0][i] = 1;
	}
	for( int i = 2 ; i <= 10 ; i++ ){
		for( int j = 2 ; j <= 10 ; j++ ){
			if( i >= j ){
				a[i][j] = a[i-j][j] + a[i][j-1];
			}else a[i][j] = a[i][i];
		}
	}
	while( T-- ){
		cin >> m >> n ;
		cout << a[m][n] << endl;
	}
	return 0 ;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325765719&siteId=291194637