Skip to content

Latest commit

 

History

History
30 lines (26 loc) · 709 Bytes

bc.md

File metadata and controls

30 lines (26 loc) · 709 Bytes

Binomial Coefficients

struct BinomialCoefficients{
    vector<int> fact, inverse;

    BinomialCoefficients(int n){
        fact = vector<int>(n+1, 1);
        inverse = vector<int>(n+1, 1);
        for(int i = 2 ; i <= n ; i++){
            fact[i] = 1LL * fact[i-1] * i % mod;
            inverse[i] = getPower(fact[i], mod-2);
        }
    }

    int getPower(int x, int y){
        int res = 1;
        while(y){
            if(y & 1) res = 1LL * res * x % mod;
            y >>= 1;
            x = 1LL * x * x % mod;
        }
        return res;
    }
    
    inline int getNcr(int n, int r){
        return (n >= r ? 1LL * fact[n] * inverse[r] % mod * inverse[n-r] % mod : 0); 
    }
};