快速幂
求 a b m o d p ( 1 ≤ a , b , p ≤ 1 0 18 ) a^b mod p (1 \le a, b, p \le 10^{18}) abmodp(1≤a,b,p≤1018)
ll quick_power(ll a, ll b, ll p) { // ll 是long long类型
ll ans = 1 % p;
for(; b; b >>= 1) {
if(b & 1) ans = ans * a % p;
a = a * a % p;
}
return ans;
}
龟速乘(解法一)
求 a ∗ b m o d p ( 1 ≤ a , b , p ≤ 1 0 18 ) a*b \ mod p (1 \le a, b, p \le 10^{18}) a∗b modp(1≤a,b,p≤1018)
ll quick_mul(ll a, ll b, ll p) { // ll 是long long类型
ll ans = 0;
for(; b; b >>= 1) {
if(b & 1) ans = (ans + a) % p;
a = a * 2 % p;
}
return ans;
}
光速乘(解法二)
求 a ∗ b m o d p ( 1 ≤ a , b , p ≤ 1 0 18 ) a*b \ mod p (1 \le a, b, p \le 10^{18}) a∗b modp(1≤a,b,p≤1018)
ull mul(ull a, ull b, ull p) { // ull 是 unsigned long long 类型,ll 是long long类型
a %= p, b %= p;
ull c = (long double)a * b / p;
ull x = a * b, y = c * p;
ll ans = (long long)(x % p) - (long long)(y % p);
if(ans < 0) ans += p;
return ans;
}