Description
现有r个互不相同的盒子和n个互不相同的球,要将这n个球放入r个盒子中,且不允许有空盒子。则有多少种放法?
Input
n, r(0 <= n, r <= 10)。
Output
有多少种放法。
Sample Input
3 2
Sample Output
6
第二类斯特林数求解
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int fac(int n){
int ans = 1;
for(int i = 2; i <= n; i++){
ans *= i;
}
return ans;
}
int main(){
int m, n;
long long a[11][11];
memset(a, 0, sizeof(a));
scanf("%d%d", &m, &n);
for(int i = 1; i <= n; i++){
a[i][i] = 1;
}
for(int i = 1; i <= n; i++){
for(int j = i+1; j <= m; j++)
a[i][j] = a[i-1][j-1] + i * a[i][j-1];
}
printf("%d",fac(n) * a[n][m]);
return 0;
}