首页 > 其他分享 >求组合数的三种方法

求组合数的三种方法

时间:2023-04-14 18:12:33浏览次数:34  
标签:return infact 组合 int res LL 三种 方法 mod

 

 

#include<bits/stdc++.h>
using namespace std;
const int N=2010,mod=1e9+7;
int c[N][N];
 void inti(){
     for(int i=0;i<N;i++){
         for(int j=0;j<=i;j++){
             if(j==0) c[i][j]=1;
             else 
             {
                 c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
             }
         }
     }
 }
int main(){
    int n;
    cin>>n;
    inti();
while(n--){
    int a,b;
    cin>>a>>b;
     printf("%d\n",c[a][b]);
}
    return 0;
}

 
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10,mod=1e9+7;
int infact[N],fact[N];
 int qmi(int a, int k, int p)
{
    int res = 1;
    while (k)
    {
        if (k & 1) res = (LL)res * a % p;
        a = (LL)a * a % p;
        k >>= 1;
    }
    return res;
}

int main(){
    int n;
    cin>>n;
     fact[0] = infact[0] = 1;
    for (int i = 1; i < N; i ++ )
    {
        fact[i] = (LL)fact[i - 1] * i % mod;
        infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
    }
    while(n--){
        int a,b;
        cin>>a>>b;
        printf("%d\n", (LL)fact[a] * infact[b] % mod * infact[a - b] % mod);
    }
    return 0;
}

 

 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int p;
int qmi(LL a,int k){
    int res=1;
    while(k){
        if(k&1)  res=(LL)res*a%p;
        a=(LL)a*a%p;
        k>>=1;
    }
    return res;
}
int C(LL a,LL b){
    if(b>a) return 0;
    int res=1;
    for(int i=1,j=a;i<=b;i++,j--){
        res=(LL)res*j%p;
         res = (LL)res * qmi(i, p - 2) % p;
    }
    return res;
}
int lucas(LL a,LL b){
    if(a<p&&b<p) return C(a,b);
    return (LL)C(a%p,b%p)*lucas(a/p,b/p)%p;
}
int main(){
    int n;
    cin>>n;
    while(n--){
        LL a,b;
        cin>>a>>b>>p;
        cout<<lucas(a,b)<<endl;
    }
    return 0;
    }

 

标签:return,infact,组合,int,res,LL,三种,方法,mod
From: https://www.cnblogs.com/aixin52129211/p/17319193.html

相关文章

  • Ubuntu开机卡“A start job is running for wait for network to be Configured”的解
    问题虚拟机安装ubuntu22.04TLS系统后,开机总会卡在等待网络连接好长时间。卡在AstartjobisrunningforhaitforNetworktobeConfigured(1min40s/no)这里如图所示解决办法进入系统后,打开终端,输入下面命令,cd/etc/systemd/system/network-online.target.wants/......
  • 我用这些方法+工具,3天完成了原本一周的工作
    随着互联网技术的发展,项目管理的方法与工具在不断丰富,如何通过方法+工具来帮助提升项目管理效率,是每个职场人都迫切需求的。我本身作为一个项目负责人,深刻体会到项目管理是一项困难的任务。每天都需要通过微信或开会与各个成员进行沟通,而且随着项目的增多,进度的掌控和管理也会变......
  • 飞项的5种应用方法,帮助你轻松学会项目管理!
    随着时代的更新变化,在现代企业中,项目管理已经成为一项非常重要的能力考核。而对于刚开始入门项目管理的新手,很多都不知道从哪里入手,怎么入手。同执行者相比,管理者所思考的维度又大不相同,接下来我们就来聊聊,新手如何使用飞项工具来学会项目管理。资深的项目管理者,一般会考虑以下......
  • JavaScript 中 new Date().getTime() 方法在 iOS 中的兼容性问题
    JavaScript中newDate(time).getTime()获取时间戳方法在iOS中的兼容性问题在iOS系统的H5页面中获取时间戳方法newDate(time).getTime()存在返回NaN或结果不准确的情况在iPhone8中iOS11.03系统下的H5页面测试newDate(time).getTime()方法测试代码:测试结......
  • JavaScript 使用 reduce 方法实现简单的 i18n 功能
    JavaScript使用reduce方法实现简单的i18n功能i18n:国际化(Internationalization)的缩写使用Array.prototype.reduce()方法实现简单的i18n功能reduce()方法对数组中的每个元素按序执行一个由您提供的reducer函数,每一次运行reducer会将先前元素的计算结果作为参......
  • python3 静态方法,类方法和普通方法
    classMyClass:@staticmethoddefstatic_method():print('静态方法')@classmethoddefclass_method(cls):print('类方法')#普通方法defnormal_method(self):print('普通方法')MyClass.stati......
  • python + QXDM5高通平台通过自动化截取log方法及代码
    转自python+QXDM5高通平台通过自动化截取log方法及代码准备工作:1.安装QXDM52.配置adb环境变量3.使用python3以上4.完成代码如下1importsys2importtime3importos45ifsys.platform.startswith("linux"):6sys.path.append('/opt/qcom/QXD......
  • 储存功能方法封装与使用说明
    /**设置缓存**/exportfunctionlocalStorageSetItem(name,data){window.localStorage.setItem(name,data)}/**获取缓存**/exportfunctionlocalStorageGetItem(data){returnwindow.localStorage.getItem(data);}/**删除缓存**/exportfunctionloc......
  • React-DnD的简要使用方法与API文档
    前提它这个官方文档贼难进去,而且第一次看的时候也不太好理解,这篇文章就把一些常用的内容记下,希望能帮助到大家。本篇文章参考的是16.0.1版本npmireact-dnd1简单示例先不说具体API,来看下常用示例~1.1useDrag:让DOM允许拖拽importReactfrom'react'import{useDrag}......
  • 批量改名高手软件批量重命名TXT文档文件的方法
    最近有很多朋友在问,如何管理文件?比如说各种形式的文件该怎么批量重命名呢?今天小编能给大家分享一个新的处理技巧,下面一起来试试。材料准备:一台Win系统的电脑安装一个文件批量改名高手文件素材若干步骤演示:步骤1:打开【文件批量改名高手】,在“文件批量重命名”中,单击“添加文件”步骤......