求组合数Ⅲ
20万组数据, 1 ≤ b ≤ a ≤ 1 0 18 , 1 ≤ p ≤ 1 0 5 1 \le b \le a \le 10^{18}, 1\le p \le 10 ^5 1≤b≤a≤1018,1≤p≤105,使用卢卡斯定理。
卢卡斯定理:
C
a
b
≡
C
a
m
o
d
p
b
m
o
d
p
C
a
/
p
b
/
p
(
m
o
d
p
)
C_a^b \equiv C_{a mod p}^{b mod p}C_{a /p}^{b / p} (mod p)
Cab≡CamodpbmodpCa/pb/p(modp)
时间复杂度为
O
(
l
o
g
p
N
⋅
p
l
o
g
p
)
O(log _p N \cdot plogp)
O(logpN⋅plogp)
题目
代码
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
int p;
int qmi(int a, int k){
int res = 1;
while(k) {
if(k & 1) res = (LL) res * a % p;
a = (LL) a * a % p;
k >>= 1;
}
return res;
}
int C(int a, int b) {
int res = 1;
for(int i = 1, j = a; i <= b; i ++, j --) {
res = (LL) res * j % p;
res = (LL) res * qmi(i, p - 2) % p;
}
return res;
}
int lucas(LL a, LL b) {
if(a < p && b < p) return C(a, b);
return (LL)C(a % p, b % p) * lucas(a / p, b / p) % p;
}
int main() {
int n;
cin >> n;
while(n -- ) {
LL a, b;
cin >> a >> b >> p;
cout << lucas(a, b) << endl;
}
return 0;
}
标签:le,return,int,res,LL,算法,基础课,mod,AcWing
From: https://blog.csdn.net/Sophia2021XJTU/article/details/139899189