competitive-cpp

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub fiore57/competitive-cpp

:warning: math/mypow.cpp

Back to top page

Code

ll myPow(ll x, ll n, const ll mod = -1) {
    ll ret = 1;
    if (mod > 0) {
        while (n > 0) {
            if (n & 1)
                ret = (ret * x) % mod;
            x = (x * x) % mod;
            n >>= 1;
        }
    } else {
        while (n > 0) {
            if (n & 1)
                ret = ret * x;
            x *= x;
            n >>= 1;
        }
    }
    return ret;
}

#line 1 "math/mypow.cpp"
ll myPow(ll x, ll n, const ll mod = -1) {
    ll ret = 1;
    if (mod > 0) {
        while (n > 0) {
            if (n & 1)
                ret = (ret * x) % mod;
            x = (x * x) % mod;
            n >>= 1;
        }
    } else {
        while (n > 0) {
            if (n & 1)
                ret = ret * x;
            x *= x;
            n >>= 1;
        }
    }
    return ret;
}

Back to top page