您的位置:首页 > 其它

hdu 1521 排列组合 指数型母函数

2016-06-20 21:45 357 查看
从n种物品中选出m个物品,求排列数。每个物品均有相应数目。
由于要考虑同类物品排列时的重复情况,必须用指数型母函数。


排列组合

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 3477    Accepted Submission(s): 1453


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   |   We have carefully selected several similar problems for you:  1085 1171 1398 2152 2110 

 

Statistic | Submit | Discuss | Note

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 10   ;
int num[maxn+5],n,m;
int a[maxn+5],b[maxn+5];
int C[maxn+5][maxn+5],A[maxn+5];
void work()
{
memset(a,0,sizeof a);//不可少
for(int p=0;p<=m&&p<=num[1];p++)
{
a[p]=A[m]/A[p];
}

for(int i=2;i<=n;i++)
{
for(int p=0;p<=m&&p<=num[i];p++)
{
for(int tp=p;tp<=m;tp++)
{
b[tp]+=a[tp-p]/A[p] ;
}

}
for0(p,m+1)
{
a[p]=b[p];
b[p]=0;
}
}
printf("%d\n",a[m]);

}

void pre()
{
C[0][0]=1;A[0]=1;
for(int i=1;i<=maxn;i++)
{
for(int j=1;j<i;j++)
{
C[i][j]=C[i-1][j-1]+C[i-1][j];
}
A[i]=A[i-1]*i;
}
}

int main()
{
pre();
while(~scanf("%d%d",&n,&m))
{
for1(i,n)
{
scanf("%d",&num[i]);
}
work();

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: