diff --git a/template.cc b/template.cc index 92b5fcd..e0ac325 100644 --- a/template.cc +++ b/template.cc @@ -279,6 +279,16 @@ return_t qpow(ll b, ll p) { else return half * half; } +// dynamic modulus +ll qpow(ll b, ll p, ll mod) { + if (b == 0 and p != 0) return 0; + if (p == 0) return 1; + ll half = qpow(b, p / 2, mod); + if (p % 2 == 1) return (int128(half) * half % mod) * b % mod; + else return half * half % mod; +} + + // Accurately find `i` 'th root of `n` (taking the floor) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wparentheses"