1. 程式人生 > >逆元求組合數

逆元求組合數

print while code pri n! long -m () mod

long long pow_mod(long long x, long long n, long long mod) 
{
    long long res = 1;
    while (n) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
long long fac[Max];
long long n, m, p;
int main() {
    while (~scanf("
%lld %lld", &n, &m)) { p=1e9+7; fac[0] = 1; for (int i = 1; i <= n; i++) {fac[i] = fac[i - 1] * i % p;} //組合數 = n!*(m!%p的逆元)*((n-m)!%p的逆元)%p printf("%lld\n", fac[n] * pow_mod(fac[m], p - 2, p) % p * pow_mod(fac[n - m], p - 2, p) % p); } }

逆元求組合數