O( nlogn )預處理, O( 1 ) 求組合數模板

typedef long long LL;
const int maxn = 5e5;
const long long mod = 1e9 + 7;
LL A[maxn];
LL B[maxn];
void Init()
{
    A[0] = 1;
    for(int i=1; i<=maxn; i++){
        A[i] = (A[i-1] * i ) % mod;
    }
}

LL Ext_Gcd(LL a, LL b, LL &x, LL &y)
{
    if(b==0){
        x=1;
        y=0;
        return a;
    }
    LL d = Ext_Gcd(b, a%b, y, x);
    y-=a/b*x;
    return d;
}

LL Inv(LL a, LL n)
{
    LL x,y;
    LL d = Ext_Gcd(a,n,x,y);
    if(d == 1)
        return ((x%n)+n)%n;
    return -1;
}
LL get()
{
    for(int i=0;i<maxn;i++)
        B[i] = Inv(A[i],mod);
}
LL C(LL a, LL b, LL mod)
{
    if(a < b)
        return 0;
    return (A[a] * B[b] %mod)*B[a-b]%mod;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章