概率论中的组合数应该比较熟悉吧,在数论中组合数也具有重大意义,下面介绍组合数的解法:

方法一O(n^2):

利用公式(n,m)=(n-1,m-1)+(n-1,m):

模板:

  1. #include<cstdio>
  2. const int N = + ;
  3. const int MOD = (int)1e9 + ;
  4. int comb[N][N];//comb[n][m]就是C(n,m)
  5. void init(){
  6. for(int i = ; i < N; i ++){
  7. comb[i][] = comb[i][i] = ;
  8. for(int j = ; j < i; j ++){
  9. comb[i][j] = comb[i-][j] + comb[i-][j-];
  10. comb[i][j] %= MOD;
  11. }
  12. }
  13. }
  14. int main(){
  15. init();
  16. }

方法二(O(n)):

因为大部分题都有求余,所以我们大可利用逆元的原理(没求余的题目,其实你也可以把MOD自己开的大一点,这样一样可以用逆元做)。利用公式:

我们需要求阶乘和逆元阶乘。

模板:

  1. const int MOD = (int)1e9 + ;
  2. int F[N], Finv[N], inv[N];//F是阶乘,Finv是逆元的阶乘
  3. void init(){
  4. inv[] = ;
  5. for(int i = ; i < N; i ++){
  6. inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
  7. }
  8. F[] = Finv[] = ;
  9. for(int i = ; i < N; i ++){
  10. F[i] = F[i-] * 1ll * i % MOD;
  11. Finv[i] = Finv[i-] * 1ll * inv[i] % MOD;
  12. }
  13. }
  14. int comb(int n, int m){//comb(n, m)就是C(n, m)
  15. if(m < || m > n) return ;
  16. return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
  17. }
  18. int main(){
  19. init();
  20. }

求组合数的O(n^2)和O(n)解法及模板的更多相关文章

  1. lucas求组合数C(n,k)%p

    Saving Beans http://acm.hdu.edu.cn/showproblem.php?pid=3037 #include<cstdio> typedef __int64 L ...

  2. URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理

    URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...

  3. 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)( ...

  4. 求组合数、求逆元、求阶乘 O(n)

    在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...

  5. HDU 5852 Intersection is not allowed!(LGV定理行列式求组合数)题解

    题意:有K个棋子在一个大小为N×N的棋盘.一开始,它们都在棋盘的顶端,它们起始的位置是 (1,a1),(1,a2),...,(1,ak) ,它们的目的地是 (n,b1),(n,b2),...,(n,b ...

  6. 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 ...

  7. 求组合数 C++程序

    一 递归求组合数 设函数为void    comb(int m,int k)为找出从自然数1.2.... .m中任取k个数的所有组合. 分析:当组合的第一个数字选定时,其后的数字是从余下的m-1个数中 ...

  8. HDU 5698——瞬间移动——————【逆元求组合数】

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  9. 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 ...

随机推荐

  1. Mybatis学习2传统dao开发

    传统dao开发 在mybati基础上 dao和daoimpl 1.工厂工具类 获得SqlSessionFactory SqlSessionFactoryUtil.java package util; ...

  2. leetcode1006

    func clumsy(N int) int { var ary []int ; n-- { ary = append(ary, n) } re := N % //4个数字一组 firstgroup ...

  3. 塔式Server 服务器ESXI6.5安装

    参考文献: https://www.cnblogs.com/yufusec/p/9181422.html 第一步: esxi6.5.ios文件的下载 第二步: 通过UltraISO软件 制作启动盘或光 ...

  4. innosetup 安装前、卸载前判断是否有进程正在运行<转>

    [Code] //安装前判断是否有进程正在运行,istask.dll文件与打包的exe文件一起 function RunTask(FileName: string; bFullpath: Boolea ...

  5. leetcode题解 Generate Parentheses

    原文链接:https://leetcode.com/problems/generate-parentheses 给出数字n,求n对括号组成的合法字符串. 刚做出来,敲完代码,修改了一次,然后提交,ac ...

  6. UICollectionView自定义cell布局layout

    写一个类继承UICollectionViewLayout,这个类需要提供一个数组来标识各个cell的属性信息,包括位置,size大小,返回一个UICollectionViewLayoutAttribu ...

  7. 如何使用JDBC连接Mysql数据库

    //java类名BaseDaopublic class BaseDao {    private Connection conn = null; // 声明Connection对象,Connectio ...

  8. 26.如何使用python操作我们自己创建的docker image呢?

    因为逻辑复杂 我们建个文件来 python #是单行注释 '''是多行注释 或者””” 我们想使用python来操作docker 那么就要一个api https://github.com/docker ...

  9. tomcat生成调试日志配置

    创建文件logging.properties 文件存放于应用WEB-INF/classes下 文件内容如下:  org.apache.juli.FileHandler.prefix = error-d ...

  10. swift中UIImageView的创建

    let imageView = UIImageView() let imageView1 = UIImageView(frame: CGRectMake(, , , )) // 创建出来的UIImag ...