hdu1521 排列组合 指数型母函数模板题
2024-09-01 08:04:49
排列组合
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4891 Accepted Submission(s): 2122
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
1 1
Sample Output
2
Author
xhd
Recommend
从题目中可以看出来,取m件物品可能取到重复的物品,所以这是一个指数型母函数问题
接下来就是套用母函数模板了
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iterator>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 2*1e2 + 10;
const int mod = 10000;
typedef long long ll;
ll f( ll x ) {
ll sum = 1;
for( ll i = 1; i <= x; i ++ ) {
sum *= i;
}
return sum;
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
ll n, m, num[maxn];
double a[maxn], b[maxn];
while( cin >> n >> m ) {
memset( a, 0, sizeof(a) );
memset( b, 0, sizeof(b) );
for( ll i = 0; i < n; i ++ ) {
cin >> num[i];
}
for( ll i = 0; i <= num[0]; i ++ ) {
a[i] = 1.0/f(i);
}
for( ll i = 1; i < n; i ++ ) {
for( ll j = 0; j <= n; j ++ ) {
for( ll k = 0; k <= num[i] && k+j <= n; k ++ ) {
b[k+j] += a[j]/f(k);
}
}
for( ll j = 0; j <= n; j ++ ) {
a[j] = b[j], b[j] = 0;
}
}
printf("%.lf\n",a[m]*f(m) );
}
return 0;
}
最新文章
- GridView,Repeater增加自动序号列
- java Servlet小结
- diary and html 文本颜色编辑,行距和其它编辑总汇
- 设计模式 - 策略模式(Strategy Pattern) 具体解释
- html+css布局小练习w3cfuns
- linux kernel中timer的使用
- Widget小组件
- 第6组UI组件:ViewAnimator及其子类
- python 日期 &; 时间
- Jmeter之分布式测试
- asp.net ajax get 调用(和post不一样,直接返回json才行,否则报错;post不能返回json)
- ECharts使用—折线图动态加载
- 危险代码:如何使用Unsafe操作内存中的Java类和对象
- requery.js使用姿势
- 第十一章&#160;串 (b2)蛮力匹配
- 20175310 《Java程序设计》第1周学习总结(2)
- 【转】MySQL安全配置介绍
- 终于在nowcoder爆发了的懒惰
- 学习Spring Boot:(七)集成Mybatis
- Spring RestTemplate 小结