组合数求模

 

 

LL getInv(LL x) {
    LL ret = 1;
    x %= mod;
    for (int a = mod - 2; a; a /= 2, x = x * x % mod)
        if (a % 2 == 1)
            ret = ret * x % mod;
    return ret;
}

int C(LL n, int m) {
    if (n < m)
        return 0;
    int ret = 1;
    for (LL i = n; i > n - m; -- i) 
        ret = ret * (i % mod) % mod * inv[n - i + 1] % mod;
    return ret;
}

 

适用于mod为素数 o(N)

void init()
{
    int i;
    pp[0] = 1;
    for(i = 1; i <= N-10 ; i++)
    {
        pp[i] = (pp[i-1]*i)%mod;
    }
}

LL fastmod(LL a,LL k)
{
    LL b = 1;
    while(k)
    {
        if(k&1)
        b = a*b%mod;
        a = (a%mod)*(a%mod)%mod;
        k/=2;
    }
    return b;
}

LL calc(int n,int m)
{
    return (pp[n]*fastmod((pp[m]*pp[n-m])%mod,mod-2))%mod)%mod;
}

加强版
posted @ 2014-07-23 20:29  _雨  阅读(217)  评论(0编辑  收藏  举报