模板(组合数取模、预处理阶乘逆元)

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 2e6+50;
ll fac[maxn];
ll inv[maxn];
const int mod = 1e9+7;
ll qpow(long long a,long long b)
{
    ll ans=1;
    ll k=a;
    while(b)
    {
        if(b&1)ans=ans*k%mod;
        k=k*k%mod;
        b>>=1;
    }
    return (ans+mod)%mod;
}
void init(){
    long long i;
    fac[0]=1;
    inv[0]=1;
    fac[1]=1;
    inv[1]=1;
    for (i=2;i<maxn;i++){
        fac[i]=((fac[i-1]*i)%mod+mod)%mod;
        inv[i]=(qpow(fac[i],mod-2)+mod)%mod;
    }
}
ll C(ll n,ll m){
    if (n<m||m<0) return 0;
    return  (fac[n]*inv[m]%mod*inv[n-m]%mod+mod)%mod;
}
int main()
{
	init();
	cout<<C(4,2)<<endl;
}

posted @ 2020-07-02 15:23  特立独行的猪猪  阅读(208)  评论(0编辑  收藏  举报