[HDU 1521] 排列组合 指数型母函数
2015-07-29 20:06
344 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1521
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
思路:排列问题可以用指数型的母函数求解
f(x)= ( 1 + x +x^2 / 2! + x^3 / 3! +…..+ x^n1 / n1! ) (1+ x +x^2/ 2!+ x^3 / 3! +…..+ x^n2 / n2! ) ……….( 1 + x +x^2 / 2! + x^3 / 3! +…..+ x^nk / nk! )
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
思路:排列问题可以用指数型的母函数求解
f(x)= ( 1 + x +x^2 / 2! + x^3 / 3! +…..+ x^n1 / n1! ) (1+ x +x^2/ 2!+ x^3 / 3! +…..+ x^n2 / n2! ) ……….( 1 + x +x^2 / 2! + x^3 / 3! +…..+ x^nk / nk! )
[code]#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800}; int main() { int n, m; int counts[12]; double nex[12], temp[12]; while(cin>>n>>m){ for(int i = 0; i < n; i++){ cin>>counts[i]; } for(int i = 0; i <= 10; i++){ nex[i] = 0.0; temp[i] = 0.0; } for(int i = 0; i <= counts[0]; i++){ nex[i] = 1.0 / fac[i]; } for(int i = 1; i < n; i++){ for(int j = 0; j <= m; j++){ for(int k = 0; k <= counts[i] && j+k <= m; k++){ temp[j+k] += nex[j]/fac[k]; } } for(int j = 0; j <= m; j++){ nex[j] = temp[j]; temp[j] = 0; } } printf("%.0lf\n", nex[m]*fac[m]); } return 0; }
相关文章推荐
- LeetCode || Validate Binary Search Tree
- Detecting diabetic retinopathy in eye images
- POJ 1679 The Unique MST
- c++ 11 新特性
- 18.4Sum (Map)
- #1128 : 二分·二分查找
- hls 类型
- HDOJ 1863 畅通工程(最小生成树--kruskal)
- 压缩感知在目标跟踪的应用
- 压缩感知在目标跟踪的应用
- 开启mediawiki其他非默认功能
- Windows突然不能访问某些网址
- Python 用 os.walk 遍历目录
- 归并排序
- Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)(转)
- oracle bi publisher报表插入图片
- 把握linux内核设计思想(六):内核时钟中断
- CUDA 中解决 critical section 的一些策略
- Android四大组件
- URI和URL的区别