求组合数的O(n^2)和O(n)解法及模板
概率论中的组合数应该比较熟悉吧,在数论中组合数也具有重大意义,下面介绍组合数的解法:
方法一O(n^2):
利用公式(n,m)=(n-1,m-1)+(n-1,m):
模板:
- #include<cstdio>
- const int N = + ;
- const int MOD = (int)1e9 + ;
- int comb[N][N];//comb[n][m]就是C(n,m)
- void init(){
- for(int i = ; i < N; i ++){
- comb[i][] = comb[i][i] = ;
- for(int j = ; j < i; j ++){
- comb[i][j] = comb[i-][j] + comb[i-][j-];
- comb[i][j] %= MOD;
- }
- }
- }
- int main(){
- init();
- }
方法二(O(n)):
因为大部分题都有求余,所以我们大可利用逆元的原理(没求余的题目,其实你也可以把MOD自己开的大一点,这样一样可以用逆元做)。利用公式:
我们需要求阶乘和逆元阶乘。
模板:
- const int MOD = (int)1e9 + ;
- int F[N], Finv[N], inv[N];//F是阶乘,Finv是逆元的阶乘
- void init(){
- inv[] = ;
- for(int i = ; i < N; i ++){
- inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
- }
- F[] = Finv[] = ;
- for(int i = ; i < N; i ++){
- F[i] = F[i-] * 1ll * i % MOD;
- Finv[i] = Finv[i-] * 1ll * inv[i] % MOD;
- }
- }
- int comb(int n, int m){//comb(n, m)就是C(n, m)
- if(m < || m > n) return ;
- return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
- }
- int main(){
- init();
- }
求组合数的O(n^2)和O(n)解法及模板的更多相关文章
- lucas求组合数C(n,k)%p
Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037 #include<cstdio> typedef __int64 L ...
- URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理
URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...
- N!分解质因子p的个数_快速求组合数C(n,m)
int f(int n,int p) { ) ; return f(n/p,p) + n/p; } https://www.xuebuyuan.com/2867209.html 求组合数C(n,m)( ...
- 求组合数、求逆元、求阶乘 O(n)
在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...
- HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解
题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...
- hdu 2519 求组合数
求组合数 如果求C5 3 就是5*4*3/3*2*1 也就是(5/3)*(4/2)*(3/1) Sample Input5 //T3 2 //C3 25 34 43 68 0 Sample Outpu ...
- 求组合数 C++程序
一 递归求组合数 设函数为void comb(int m,int k)为找出从自然数1.2.... .m中任取k个数的所有组合. 分析:当组合的第一个数字选定时,其后的数字是从余下的m-1个数中 ...
- HDU 5698——瞬间移动——————【逆元求组合数】
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
随机推荐
- Mybatis学习2传统dao开发
传统dao开发 在mybati基础上 dao和daoimpl 1.工厂工具类 获得SqlSessionFactory SqlSessionFactoryUtil.java package util; ...
- leetcode1006
func clumsy(N int) int { var ary []int ; n-- { ary = append(ary, n) } re := N % //4个数字一组 firstgroup ...
- 塔式Server 服务器ESXI6.5安装
参考文献: https://www.cnblogs.com/yufusec/p/9181422.html 第一步: esxi6.5.ios文件的下载 第二步: 通过UltraISO软件 制作启动盘或光 ...
- innosetup 安装前、卸载前判断是否有进程正在运行<转>
[Code] //安装前判断是否有进程正在运行,istask.dll文件与打包的exe文件一起 function RunTask(FileName: string; bFullpath: Boolea ...
- leetcode题解 Generate Parentheses
原文链接:https://leetcode.com/problems/generate-parentheses 给出数字n,求n对括号组成的合法字符串. 刚做出来,敲完代码,修改了一次,然后提交,ac ...
- UICollectionView自定义cell布局layout
写一个类继承UICollectionViewLayout,这个类需要提供一个数组来标识各个cell的属性信息,包括位置,size大小,返回一个UICollectionViewLayoutAttribu ...
- 如何使用JDBC连接Mysql数据库
//java类名BaseDaopublic class BaseDao { private Connection conn = null; // 声明Connection对象,Connectio ...
- 26.如何使用python操作我们自己创建的docker image呢?
因为逻辑复杂 我们建个文件来 python #是单行注释 '''是多行注释 或者””” 我们想使用python来操作docker 那么就要一个api https://github.com/docker ...
- tomcat生成调试日志配置
创建文件logging.properties 文件存放于应用WEB-INF/classes下 文件内容如下: org.apache.juli.FileHandler.prefix = error-d ...
- swift中UIImageView的创建
let imageView = UIImageView() let imageView1 = UIImageView(frame: CGRectMake(, , , )) // 创建出来的UIImag ...