1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| template <typename T> T qpower(T a, i64 b, T init = T(), int mod = 0) { T res = init; if (!mod) { while (b > 0) { if (b & 1) { res = res * a; } a = a * a; b >>= 1; } } else { while (b > 0) { if (b & 1) { res = res * a % mod; } a = a * a % mod; b >>= 1; } } return res; }
|