排列组合[HDU1521]

排列组合

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[15],frac[15],tmp[15];

int c[15];

int n,m;

int main()

{

	int n,m,i,j,k;

	frac[0]=1;

	for (i=1;i<12;i++) frac[i]=frac[i-1]*i;

	while (scanf("%d%d",&n,&m)!=EOF)

	{

		for (i=1;i<=n;i++) scanf("%d",&c[i]);

		memset(p,0,sizeof(p));

		for (i=0;i<=c[1];i++) p[i]=1.0/frac[i];

		for (i=2;i<=n;i++)

		{

			memset(tmp,0,sizeof(tmp));

			for (j=0;j<=m;j++)

				for (k=0;k<=c[i];k++)

					tmp[j+k]+=p[j]*(1.0/frac[k]);

			for (j=0;j<=m;j++) p[j]=tmp[j];

		}

		printf("%.0lf\n",p[m]*frac[m]);

	}

	return 0;

}

 

 

 

你可能感兴趣的:(HDU)