排列组合
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1736 Accepted Submission(s): 726
Problem Description
有n种物品,并且知道每种物品的数量。要求从中选出m件物品的排列数。例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB","BA"两种。
Input
每组输入数据有两行,第一行是二个数n,m(1<=m,n<=10),表示物品数,第二行有n个数,分别表示这n件物品的数量。
Output
对应每组数据输出排列数。(任何运算不会超出2^31的范围)
Sample Input
2 2
1 1
Sample Output
2
Author
xhd
Recommend
xhd
A typical application of exponential generating function.The i-th good's generating function is (1+x/1!+x^2/2!+......+x^C[i]/C[i]!).In the product of the n functions,the coefficient of the item whose index of x is n multiplies n! is the finally answer.
#include<stdio.h>
#include<string.h>
double p[],frac[],tmp[];
int c[];
int n,m;
int main()
{
int n,m,i,j,k;
frac[]=;
for (i=;i<;i++) frac[i]=frac[i-]*i;
while (scanf("%d%d",&n,&m)!=EOF)
{
for (i=;i<=n;i++) scanf("%d",&c[i]);
memset(p,,sizeof(p));
for (i=;i<=c[];i++) p[i]=1.0/frac[i];
for (i=;i<=n;i++)
{
memset(tmp,,sizeof(tmp));
for (j=;j<=m;j++)
for (k=;k<=c[i];k++)
tmp[j+k]+=p[j]*(1.0/frac[k]);
for (j=;j<=m;j++) p[j]=tmp[j];
}
printf("%.0lf\n",p[m]*frac[m]);
}
return ;
}