CppLibrary

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

View the Project on GitHub Tiramister/CppLibrary

:heavy_check_mark: Verify/mod_pow.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/6/NTL/1/NTL_1_B"

#include "../Number/modint.hpp"

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;

int main() {
    std::cin.tie();
    std::ios::sync_with_stdio(false);

    int m, n;
    std::cin >> m >> n;
    std::cout << mint(m).pow(n) << "\n";

    return 0;
}
#line 1 "Verify/mod_pow.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/6/NTL/1/NTL_1_B"

#line 2 "Number/modint.hpp"

#include <iostream>

template <int MOD>
struct ModInt {
    using lint = long long;
    int val;

    // constructor
    ModInt(lint v = 0) : val(v % MOD) {
        if (val < 0) val += MOD;
    };

    // unary operator
    ModInt operator+() const { return ModInt(val); }
    ModInt operator-() const { return ModInt(MOD - val); }

    ModInt& operator++() { return *this += 1; }
    ModInt& operator--() { return *this -= 1; }

    // functions
    ModInt pow(lint n) const {
        auto x = ModInt(1);
        auto b = *this;
        while (n > 0) {
            if (n & 1) x *= b;
            n >>= 1;
            b *= b;
        }
        return x;
    }
    ModInt inv() const {
        int s = val, t = MOD,
            xs = 1, xt = 0;

        while (t != 0) {
            auto div = s / t;

            s -= t * div;
            xs -= xt * div;

            std::swap(s, t);
            std::swap(xs, xt);
        }

        return xs;
    }

    // arithmetic
    ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }
    ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }
    ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }
    ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }

    ModInt& operator+=(const ModInt& x) {
        if ((val += x.val) >= MOD) val -= MOD;
        return *this;
    }
    ModInt& operator-=(const ModInt& x) {
        if ((val -= x.val) < 0) val += MOD;
        return *this;
    }
    ModInt& operator*=(const ModInt& x) {
        val = lint(val) * x.val % MOD;
        return *this;
    }
    ModInt& operator/=(const ModInt& x) { return *this *= x.inv(); }

    // comparator
    bool operator==(const ModInt& b) const { return val == b.val; }
    bool operator!=(const ModInt& b) const { return val != b.val; }

    // I/O
    friend std::istream& operator>>(std::istream& is, ModInt& x) {
        lint v;
        is >> v;
        x = v;
        return is;
    }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& x) {
        return os << x.val;
    }
};

using modint1000000007 = ModInt<1000000007>;
using modint998244353 = ModInt<998244353>;
#line 4 "Verify/mod_pow.test.cpp"

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;

int main() {
    std::cin.tie();
    std::ios::sync_with_stdio(false);

    int m, n;
    std::cin >> m >> n;
    std::cout << mint(m).pow(n) << "\n";

    return 0;
}
Back to top page