预处理组合数和逆元o(n)

int fact[N], infact[N];
int qpow(int a, int b)
{
    int res = 1;
    while (b)
    {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
void init()
{
    fact[0] = 1;
    for (int i = 1; i <= N; i++)
        fact[i] = fact[i - 1] * i % mod;
    infact[N] = qpow(fact[N], mod - 2);
    for (int i = N; i >= 1; i--)
        infact[i - 1] = infact[i] * i % mod;
}
int C(int n, int m)
{
    return fact[n] * infact[m] % mod * infact[n - m] % mod;
}

你可能感兴趣的:(java,算法,数据结构)