返回顶部

模板 - 卢卡斯定理

const int MOD = 10007;

ll n, m;

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 C(ll n, ll m) {
    if(m > n)
        return 0;
    ll up = 1, down = 1;
    for(ll i = n - m + 1; i <= n; ++i)
        up = up * i % MOD;
    for(ll i = 1; i <= m; ++i)
        down = down * i % MOD;
    return up * qpow(down, MOD - 2) % MOD;
}

ll Lucas(ll n, ll m) {
    if(m > n)
        return 0;
    ll ans = 1;
    for(; m; n /= MOD, m /= MOD)
        ans = ans * C(n % MOD, m % MOD) % MOD;
    return ans;
}
posted @ 2019-10-07 18:31  Inko  阅读(...)  评论(...编辑  收藏