【数学】组合数学

模版

namespace Combinatory {

#define COMBINATORY_AUTO_INIT

// MOD must be a prime and bigger than MAXN
static const int MOD = 1e9 + 7;
static const int MAXN = 2e6 + 10;

ll qpow (ll x, ll n) {
    ll res = 1;
    while (n) {
        if (n & 1) {
            res = res * x % MOD;
        }
        x = x * x % MOD;
        n >>= 1;
    }
    return res;
}

ll inv (ll x) {
    return qpow (x, MOD - 2);
}

ll fac[MAXN];
ll inv_fac[MAXN];

void init_combinatory () {
    int n = MAXN - 5;
    assert (1 <= n && n <= MAXN && MAXN < MOD);

    fac[0] = 1;
    for (int i = 1; i <= n; ++i) {
        fac[i] = 1LL * fac[i - 1] * i % MOD;
    }
    inv_fac[n] = inv (fac[n]);
    for (int i = n - 1; i >= 0; --i) {
        inv_fac[i] = 1LL * inv_fac[i + 1] * (i + 1) % MOD;
    }
}

ll A (ll n, ll m) {
    assert (fac[0] > 0 && n <= MAXN);
    if (n < 0 || m < 0 || n < m) {
        return 0;
    }
    return 1LL * fac[n] * inv_fac[n - m] % MOD;
}

ll C (ll n, ll m) {
    assert (fac[0] > 0 && n <= MAXN);
    if (n < 0 || m < 0 || n < m) {
        return 0;
    }
    return 1LL * A (n, m) * inv_fac[m] % MOD;
}

}

using namespace Combinatory;

原理

排列组合,隔板法,组合数的公式,圆排列,错位排列,二项式定理:https://www.cnblogs.com/purinliang/p/18130905
卡特兰数:https://www.cnblogs.com/purinliang/p/18130909

posted @ 2020-11-15 13:40  purinliang  阅读(287)  评论(0编辑  收藏  举报