From 3526e2ac11b7a9fdb78d3fc0ecd7c79af175f761 Mon Sep 17 00:00:00 2001 From: subcrip Date: Wed, 4 Dec 2024 17:07:31 +0000 Subject: [PATCH] Update template.cc Signed-off-by: subcrip --- template.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) 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"