组合数C(m,n)的四种求法

递推、杨辉三角

#include<bits/stdc++.h>
using namespace std;

const int mod = 1e9+7, N = 2010;
int n,a,b,c[N][N];

int main(){
    for(int i=0;i<N;i++){
        for(int j=0;j<=i;j++){
            if(j==0) c[i][0] = 1;
            else c[i][j] = (long long)(c[i-1][j-1]+c[i-1][j])%mod;
        }
    }
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&a,&b);
        printf("%d\n",c[a][b]);
    }
    return 0;
}

乘法逆元、费马小定理

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mod = 1e9+7, N = 100010;
int n,a,b;
ll f[N] = {1},inf[N] = {1};
// 快速幂求 a^(p-2)
ll qp(ll a,ll b){
    ll res = 1;
    while(b){
        if(b&1) res = res*a%mod;
        a = a*a%mod;
        b >>= 1;
    }
    return res;
}
// 求阶乘,和阶乘的逆元。
void init(){
    for(int i=1;i<N;i++){
        f[i] = (f[i-1]*i)%mod;
        inf[i] = (inf[i-1])*qp(i,mod-2)%mod;
    }
}
// 组合数的阶乘公式
int C(int a,int b){
    return f[a]*inf[b]%mod*inf[a-b]%mod;
}

int main(){
    init();
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&a,&b);
        printf("%d\n",C(a,b));
    }
}

Lucas定理

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
ll a,b;
int n,p;

ll qp(ll a,ll b,ll p){
    ll res = 1;
    while(b){
        if(b&1) res = res*a%p;
        a = a*a%p;
        b >>= 1;
    }
    return res;
}
// 组合数定义
ll C(ll a,ll b){
    ll res = 1;
    for(ll i=1,j=a;i<=b;i++,j--){
        res = res*j%p;
        res = res*qp(i,p-2,p)%p;
    }
    return res;
}

ll Lucas(ll a,ll b,ll p){
    if(a<p && b<p){
        return C(a,b);
    }
    return C(a%p,b%p)*Lucas(a/p,b/p,p)%p;
}

int main(){
    scanf("%d",&n);
    while(n--){
        scanf("%lld%lld%d",&a,&b,&p);
        printf("%d\n",Lucas(a,b,p));    
    }
    return 0;
}

欧拉筛、阶乘的质因子、高精度

#include <bits/stdc++.h>
using namespace std;


const int N = 5010;

// 依次为筛得的质数数组,质数个数,组合数中质因数分解的各个质因子的指数。
int primes[N] , cnt, sump[N]; 
bool vis[N];


// 欧拉筛素数
void get_primes(int n){
    for(int i=2;i<=n;i++){
        if(!vis[i]) primes[cnt++] = i;
        for(int j=0;primes[j]*i<=n;j++){
            vis[primes[j]*i] = 1;
            if(i%primes[j]==0) break;
        }
    }
}

// 质因数分解 (求出 n! 的p因子的指数)
int getNumOfP(int n,int p){
    int res = 0;
    while(n){
        res += n/p;
        n /= p;
    }
    return res;
}

// 高精度乘法
vector<int> mul(const vector<int>& a,int b){
    vector<int> c;
    int t = 0;
    for(int i=0;i<a.size();i++){
        t += a[i]*b;
        c.push_back(t%10);
        t /= 10;
    }
    while(t){
        c.push_back(t%10);
        t /= 10;
    }
    return c;
}

int main(){
    int a,b;
    cin>>a>>b;

    get_primes(a);
    
    for(int i=0;i<cnt;i++){
        int p = primes[i];
        sump[i] = getNumOfP(a,p)-getNumOfP(b,p)-getNumOfP(a-b,p);
    }
    
    vector<int> ans = {1};
    for(int i=0;i<cnt;i++)
        for(int j=0;j<sump[i];j++)
            ans = mul(ans,primes[i]);
    
    for(int i=ans.size()-1;i>=0;i--) printf("%d",ans[i]);
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值