排列组合
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3438 Accepted Submission(s): 1439
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
指数型母函数入门题,指数型母函数主要处理排列的问题。
#include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define INF 1000000001 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 105; int n,m,a[12]; double c[MAXN],tp[MAXN]; int fx(int x) {int ret = 1;for(int i = 1; i <= x; i++){ret *= i;}return ret; } int main() {while(~scanf("%d%d",&n,&m)){for(int i = 1; i <= n; i++){scanf("%d",&a[i]);}memset(c,0,sizeof(c));memset(tp,0,sizeof(tp));for(int i = 0; i <= a[1]; i++){c[i] = 1.0 / fx(i);}for(int i = 2; i <= n; i++){for(int j = 0; j <= m; j++){for(int k = 0; k + j <= m && k <= a[i]; k ++){tp[j + k] += c[j] * 1.0 / fx(k);}}for(int j = 0; j <= m; j++){c[j] = tp[j];tp[j] = 0;}}printf("%.0lf\n",c[m] * fx(m));}return 0; }