排列组合计数&POJ306
来源:互联网 发布:刘国梁下海经商 知乎 编辑:程序博客网 时间:2024/05/29 15:24
题意:
计算C = N! / (N-M)!M!
方法一:
#include <set>#include <cmath>#include <queue>#include <stack>#include <vector>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <functional>using namespace std;typedef long long LL;LL solve(LL n, LL k){ if(k >n /2) k = n-k; LL a = 1, b = 1; for(int i = 1; i <= k; i++) { a *= (n + 1 - i); b *= i; if(a % b == 0) { a /= b; b = 1; } } return a/ b;}int main(){ //freopen("in.txt", "r", stdin); LL n, m; while(scanf("%I64d%I64d", &n, &m) && n) { printf("%I64d things taken %I64d at a time is %I64d exactly.\n", n, m, solve(n, m)); } return 0;}
方法二:利用二项式系数公式(c[i][j] = c[i - 1][j - 1] + c[i - 1][j])
#include <set>#include <cmath>#include <queue>#include <stack>#include <vector>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <functional>using namespace std;typedef long long LL;const int maxn = 110;LL c[maxn][maxn];void init(){ for(int i = 0; i < maxn; i++) { c[i][0] = 1; } for(int i = 1; i < maxn; i++) { for(int j = 1; j <= i; j++) c[i][j] = c[i-1][j] + c[i-1][j-1]; }}int main(){ //freopen("in.txt", "r", stdin); int n, m; init(); while(scanf("%d%d", &n, &m) && n) { printf("%d things taken %d at a time is %I64d exactly.\n", n, m, c[n][m]); } return 0;}
阅读全文
0 0
- 排列组合计数&POJ306
- 排列组合&区间计数
- 一类排列组合计数问题
- 一类排列组合计数问题
- Oralce字段排列组合计数
- poj3252(排列组合计数,数位计数)
- 计数 杭电 1521 排列组合
- 排列组合计数 水poj2249 poj1306
- HDU 4055 计数dp + 排列组合
- POJ 2249-Binomial Showdown(排列组合计数)
- Bzoj 4517: [Sdoi2016]排列计数(排列组合)
- BZOJ 4517 浅谈错位排列组合计数
- [BZOJ4517][SDOI2016]排列计数(排列组合)
- 排列组合应用于计数的思路 [大三四八九月实习]
- 1211: [HNOI2004]树的计数 (prufer编码,排列组合,质因数分解)
- 【BZOJ 4517】排列计数 - 排列组合+乘法逆元
- bzoj 4517: [Sdoi2016]排列计数 排列组合+动态规划
- bzoj 2839: 集合计数 排列组合+容斥原理
- Json对象与Json字符串互转(4种转换方式)
- C# ADO.NET之SQL Server数据库操作
- [蓝桥杯]算法提高 概率计算
- Nginx--11
- rpc 双队列
- 排列组合计数&POJ306
- laravel-admin的多级联动
- bootstrap fileinput 设定图片预览长宽
- 只用13个字符完成Python编程
- 小鑫数数儿
- sourceTree中使用的一些问题
- kaggle 激活问题
- asp.net控件快捷增删改查
- 数据结构---红黑树