取模类

· · 个人记录

取模类(自用)

已知问题:

constexpr int mod = 1e9 + 7;struct getInv {auto operator()(int x) -> int {auto fpow = [](long long x, long long y, long long mod = ::mod) {x %= mod;long long res = 1;while (y != 0) {if ((y & 1) != 0) {res = res * x % mod;}x = x * x % mod;y >>= 1;}return res;};return static_cast<int>(fpow(x, mod - 2));}};template <typename Tp, int mod = ::mod, class getInv = getInv> class modInt {private:Tp val;public:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
constexpr modInt() { static_cast<int>(this->val = 0); } /* NOLINT */constexpr modInt(const Tp &val) { /* NOLINT */static_cast<int>(this->val = (val % mod + mod) % mod); /* NOLINT */}constexpr modInt(const modInt<Tp> &x) { /* NOLINT */static_cast<int>(this->val = (x.val % mod + mod) % mod); /* NOLINT */}constexpr modInt(const modInt<Tp> &&x) { /* NOLINT */static_cast<int>(this->val = (x.val % mod + mod) % mod); /* NOLINT */}
#pragma clang diagnostic pop
auto operator=(const Tp &x) -> modInt & {this->val = (x % mod + mod) % mod;return *this;}auto operator=(const modInt<Tp> &x) -> modInt & {this->val = (x.val % mod + mod) % mod;return *this;}template <class rTp> explicit operator rTp() {return static_cast<rTp>(this->val);}explicit operator bool() { return this->val != 0; }constexpr auto operator-() const -> modInt { return (-this->val) % mod; }constexpr auto operator+(const modInt<Tp> &x) const -> modInt {return ((this->val + x.val) % mod + mod) % mod;}constexpr auto operator-(const modInt<Tp> &x) const -> modInt {return ((this->val - x.val) % mod + mod) % mod;}constexpr auto operator*(const modInt<Tp> &x) const -> modInt {return ((this->val * x.val) % mod + mod) % mod;}constexpr auto operator/(const modInt<Tp> &x) const -> modInt {return ((this->val / x.val) % mod + mod) % mod;}constexpr auto operator%(const modInt<Tp> &x) const -> modInt {return ((this->val % x.val) % mod + mod) % mod;}[[nodiscard]] auto safeDiv(const modInt<Tp> &x) const -> modInt {return (this->val * getInv()(x.val)) % mod;}auto operator+=(const modInt<Tp> &x) -> modInt & {return *this = *this + x;}auto operator-=(const modInt<Tp> &x) -> modInt & {return *this = *this - x;}auto operator*=(const modInt<Tp> &x) -> modInt & {return *this = *this * x;}auto operator/=(const modInt<Tp> &x) -> modInt & {return *this = *this / x;}auto operator%=(const modInt<Tp> &x) -> modInt & {return *this = *this % x;}[[maybe_unused]] auto safeDivEqual(const modInt<Tp> &x) -> modInt & {return *this = this->safeDiv(x);}constexpr auto operator<(const modInt<Tp> &x) const -> bool {return this->val < x.val;}constexpr auto operator==(const modInt<Tp> &x) const -> bool {return this->val == x.val;}constexpr auto operator>(const modInt<Tp> &x) const -> bool {return this->val > x.val;}friend auto operator>>(std::istream &is, modInt<Tp> &x) -> std::istream & {return is >> x.val;}friend auto operator<<(std::ostream &os, const modInt<Tp> &x)-> std::ostream & {return os << x.val;}};template <class Tp>struct std::__is_integral_helper<modInt<Tp>> : public true_type {}; using modint = modInt<int>;