This documentation is automatically generated by online-judge-tools/verification-helper
#pragma once
#include <cassert>
#include <ostream>
#include <type_traits>
#include "../internal/barret_reduction.cpp"
#include "../utility/int_alias.cpp"
#include "mod_inv.cpp"
#include "rem_euclid.cpp"
template <int ID> class DynamicModint {
using Self = DynamicModint;
static inline auto bt = proconlib::BarretReduction(1);
u32 v;
public:
static u32 mod() noexcept { return bt.get_mod(); }
static void set_mod(const u32 mod) noexcept {
assert((u32)1 <= mod and mod <= ((u32)1 << 31));
bt = proconlib::BarretReduction(mod);
}
template <class T, std::enable_if_t<std::is_integral_v<T>>* = nullptr> static T normalize(const T& x) noexcept {
return rem_euclid<std::common_type_t<T, i64>>(x, mod());
}
DynamicModint() noexcept : v(0) {}
template <class T> DynamicModint(const T& x) noexcept : v(normalize(x)) {}
template <class T> static Self raw(const T& x) noexcept {
Self ret;
ret.v = x;
return ret;
}
u32 val() const noexcept { return v; }
Self neg() const noexcept { return raw(v == 0 ? 0 : mod() - v); }
Self inv() const noexcept { return raw(mod_inv(v, mod())); }
Self pow(u64 exp) const noexcept {
Self ret(1), mult(*this);
for (; exp > 0; exp >>= 1) {
if (exp & 1) ret *= mult;
mult *= mult;
}
return ret;
}
Self operator-() const noexcept { return neg(); }
Self operator~() const noexcept { return inv(); }
Self operator+(const Self& rhs) const noexcept { return Self(*this) += rhs; }
Self& operator+=(const Self& rhs) noexcept {
if ((v += rhs.v) >= mod()) v -= mod();
return *this;
}
Self operator-(const Self& rhs) const noexcept { return Self(*this) -= rhs; }
Self& operator-=(const Self& rhs) noexcept {
if (v < rhs.v) v += mod();
v -= rhs.v;
return *this;
}
Self operator*(const Self& rhs) const noexcept { return Self(*this) *= rhs; }
Self& operator*=(const Self& rhs) noexcept {
v = bt.product(v, rhs.v);
return *this;
}
Self operator/(const Self& rhs) const noexcept { return Self(*this) /= rhs; }
Self& operator/=(const Self& rhs) noexcept { return *this *= rhs.inv(); }
bool operator==(const Self& rhs) const noexcept { return v == rhs.v; }
bool operator!=(const Self& rhs) const noexcept { return v != rhs.v; }
friend std::ostream& operator<<(std::ostream& stream, const Self& rhs) { return stream << rhs.v; }
};
using Modint = DynamicModint<-1>;
#line 2 "math/dynamic_modint.cpp"
#include <cassert>
#include <ostream>
#include <type_traits>
#line 2 "utility/int_alias.cpp"
#include <cstdint>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 3 "internal/barret_reduction.cpp"
namespace proconlib {
class BarretReduction {
u32 mod;
u64 near_inv;
public:
explicit constexpr BarretReduction(const u32 mod) noexcept : mod(mod), near_inv((u64)(-1) / mod + 1) {}
constexpr u32 product(const u32 a, const u32 b) const noexcept {
const u64 z = (u64)a * b;
const u64 x = ((u128)z * near_inv) >> 64;
const u32 v = z - x * mod;
return v < mod ? v : v + mod;
}
constexpr u32 get_mod() const noexcept { return mod; }
};
} // namespace proconlib
#line 3 "math/inv_gcd.cpp"
#include <utility>
#line 3 "math/rem_euclid.cpp"
template <class T> constexpr T rem_euclid(T value, const T& mod) {
assert(mod > 0);
return (value %= mod) >= 0 ? value : value + mod;
}
#line 5 "math/inv_gcd.cpp"
template <class T> constexpr std::pair<T, T> inv_gcd(const T& a, const T& b) {
using U = std::make_signed_t<T>;
U t = rem_euclid(a, b);
if (t == 0) return {b, 0};
U s = b, m0 = 0, m1 = 1;
while (t != 0) {
const U u = s / t;
s -= t * u;
m0 -= m1 * u;
U tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0) m0 += b / s;
return {(T)s, (T)m0};
}
#line 4 "math/mod_inv.cpp"
template <class T> constexpr T mod_inv(const T& a, const T& mod) {
const auto [g, x] = inv_gcd(a, mod);
assert(g == 1);
return x;
}
#line 9 "math/dynamic_modint.cpp"
template <int ID> class DynamicModint {
using Self = DynamicModint;
static inline auto bt = proconlib::BarretReduction(1);
u32 v;
public:
static u32 mod() noexcept { return bt.get_mod(); }
static void set_mod(const u32 mod) noexcept {
assert((u32)1 <= mod and mod <= ((u32)1 << 31));
bt = proconlib::BarretReduction(mod);
}
template <class T, std::enable_if_t<std::is_integral_v<T>>* = nullptr> static T normalize(const T& x) noexcept {
return rem_euclid<std::common_type_t<T, i64>>(x, mod());
}
DynamicModint() noexcept : v(0) {}
template <class T> DynamicModint(const T& x) noexcept : v(normalize(x)) {}
template <class T> static Self raw(const T& x) noexcept {
Self ret;
ret.v = x;
return ret;
}
u32 val() const noexcept { return v; }
Self neg() const noexcept { return raw(v == 0 ? 0 : mod() - v); }
Self inv() const noexcept { return raw(mod_inv(v, mod())); }
Self pow(u64 exp) const noexcept {
Self ret(1), mult(*this);
for (; exp > 0; exp >>= 1) {
if (exp & 1) ret *= mult;
mult *= mult;
}
return ret;
}
Self operator-() const noexcept { return neg(); }
Self operator~() const noexcept { return inv(); }
Self operator+(const Self& rhs) const noexcept { return Self(*this) += rhs; }
Self& operator+=(const Self& rhs) noexcept {
if ((v += rhs.v) >= mod()) v -= mod();
return *this;
}
Self operator-(const Self& rhs) const noexcept { return Self(*this) -= rhs; }
Self& operator-=(const Self& rhs) noexcept {
if (v < rhs.v) v += mod();
v -= rhs.v;
return *this;
}
Self operator*(const Self& rhs) const noexcept { return Self(*this) *= rhs; }
Self& operator*=(const Self& rhs) noexcept {
v = bt.product(v, rhs.v);
return *this;
}
Self operator/(const Self& rhs) const noexcept { return Self(*this) /= rhs; }
Self& operator/=(const Self& rhs) noexcept { return *this *= rhs.inv(); }
bool operator==(const Self& rhs) const noexcept { return v == rhs.v; }
bool operator!=(const Self& rhs) const noexcept { return v != rhs.v; }
friend std::ostream& operator<<(std::ostream& stream, const Self& rhs) { return stream << rhs.v; }
};
using Modint = DynamicModint<-1>;