Quick power

FisherKK edited 3 年前

using namespace std;
using u64 = uint64_t;

template <typename UInt> UInt ipow(UInt const kBase, UInt const kExp, UInt const kMod) {
    UInt ret = 1;
    for (UInt i = kExp, base = kBase % kMod; i; i >>= 1) {
        if (i & 1) {
            ret = ret * base % kMod;
        }
        base = base * base % kMod;
    }
    return ret;
} // quick power algorithm for (a^b) % kMod.

Comments