【模板】盧卡斯定理
阿新 • • 發佈:2019-01-29
Lucas定理是用來求 的值。其中:, 是非負整數, 是素數。一般用於 很大而 很小或 不大且都大於 的情形。
Lucas定理結論:
其中當 時,。
其中第三步中 是應用了 是素數這一條件,不明白的可以去看看乘法逆元。
實現的時候還要考慮到組合數的幾個性質:
- 當 時
LL FastPower(LL a, LL p, LL k) {
LL ans = 1;
while (p) {
if (p & 1) ans = (ans * a) % k;
a = (a * a) % k;
p >>= 1;
}
return ans;
}
LL C(int n, int m, int p) {
if (n < m) return 0;
if (m > n - m) m = n - m;
LL s1 = 1, s2 = 1;
for (int i = 0; i < m; i++) {
s1 = s1 * (n - i) % p;
s2 = s2 * (i + 1 ) % p;
}
return s1 * FastPower(s2, p - 2, p) % p;
}
LL Lucas(int n, int m, int p) {
if (m == 0) return 1;
return C(n % p, m % p, p) * Lucas(n / p, m / p, p) % p;
}