Code Library Of Others/sgtlaugh/Binomial Coefficients Modulo Prime Powers.cpp
long long binomial(long long n, long long k, long long p, long long q){
        if (k > n) return 0;
        if (n == k || k == 0) return 1;

        int i, j;
        for (i = 0, mod = 1; i < q; i++) mod *= p;
        long long t = trailing(n, p) - trailing(k, p) - trailing(n - k, p);
        if (t >= q) return 0;

        assert(mod < MAXP);
        for (dp[0] = 1, i = 1; i < mod; i++){
            dp[i] = (long long)dp[i - 1] * (i % p ? i : 1) % mod;
        }

        long long res = factorial(n, p) * expo(factorial(k, p) * factorial(n - k, p) % mod, (mod / p) * (p - 1) - 1, mod) % mod;
        while (t--) res = res * p % mod;
        return res;
    }
/// binomial co-efficients modulo p^q (p must be a prime)
binomial co-efficients modulo p^q (p must be a prime)
binomial co - efficients modulo p^q ( p must be a prime )
long long binomial(long long n, long long k, long long p, long long q){
        if (k > n) return 0;
        if (n == k || k == 0) return 1;
        int i, j;
        for (i = 0, mod = 1; i < q; i++) mod *= p;
        long long t = trailing(n, p) - trailing(k, p) - trailing(n - k, p);
        if (t >= q) return 0;
        assert(mod < MAXP);
        for (dp[0] = 1, i = 1; i < mod; i++){
            dp[i] = (long long)dp[i - 1] * (i % p ? i : 1) % mod;
        }
        long long res = factorial(n, p) * expo(factorial(k, p) * factorial(n - k, p) % mod, (mod / p) * (p - 1) - 1, mod) % mod;
        while (t--) res = res * p % mod;
        return res;
    }
long long binomial ( long long n , long long k , long long p , long long q ) { if ( k > n ) return 0 ; if ( n == k || k == 0 ) return 1 ; int i , j ; for ( i = 0 , mod = 1 ; i < q ; i ++ ) mod *= p ; long long t = trailing ( n , p ) - trailing ( k , p ) - trailing ( n - k , p ) ; if ( t >= q ) return 0 ; assert ( mod < MAXP ) ; for ( dp [ 0 ] = 1 , i = 1 ; i < mod ; i ++ ) { dp [ i ] = ( long long ) dp [ i - 1 ] * ( i % p ? i : 1 ) % mod ; } long long res = factorial ( n , p ) * expo ( factorial ( k , p ) * factorial ( n - k , p ) % mod , ( mod / p ) * ( p - 1 ) - 1 , mod ) % mod ; while ( t -- ) res = res * p % mod ; return res ; }
binomial co-efficients modulo p^q (p must be a prime)
binomial co - efficients modulo p^q ( p must be a prime )