1715 Ball and Box
Accept: 120 Submit: 288
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
CDGG has r different boxes and n different balls.He want to put the n balls into the r boxes,and it is not allow to left empty box.How many ways can CDGG put the n balls into the r boxes?
Input
The input consists of several test cases. For each case,the only line contains two integer n and r,indicates the number of balls and the number of boxes.(0<=n,r<=10)
Output
For each case,output a line contain a integer,indicates the answer.
Sample Input
3 2
Sample Output
6
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
//第二类斯特林数
void Stirling_2(__int64 a[][25],int n)
{
//注意:不能在这里写memset(a..);这样不能对main函数中的a数组初始化
//memset(a,0,sizeof(a));
a[1][1]=1;
for(int i=2;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
a[i][j]=j*a[i-1][j]+a[i-1][j-1];
}
}
}
int main()
{
__int64 a[25][25],fac[25]={0,1};
memset(a,0,sizeof(a));
Stirling_2(a,20);
for(int i=2;i<=20;i++) fac[i]=fac[i-1]*i;
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
__int64 cnt=a[n][m]*fac[m];//important
printf("%I64d/n",cnt);
}
return 0;
}