前置知识:Barrett Reduction
我们发现这题要我们在 位内完成计算。我们只能用 Montgomery Reduction(因为 Barrett Reduction 涉及到了 位整数)。
这里放一下 hly1204 的板子:
template <uint64_t __mod, bool s_isPrime = true>
class MontgomeryModInt64 {
public:
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using m64 = MontgomeryModInt64;
using value_type = u64;
static constexpr u64 mod() { return __mod; }
static constexpr u64 get_primitive_root_prime() {
u64 tmp[64] = {};
int cnt = 0;
const u64 phi = __mod - 1;
u64 m = phi;
for (u64 i = 2; i * i <= m; ++i) {
if (m % i == 0) {
tmp[cnt++] = i;
do m /= i;
while (m % i == 0);
}
}
if (m != 1) tmp[cnt++] = m;
for (m64 res = 2;; res += 1) {
bool f = true;
for (int i = 0; i < cnt && f; ++i) f &= res.pow(phi / tmp[i]) != 1;
if (f) return u32(res);
}
}
constexpr MontgomeryModInt64() : v_() {}
~MontgomeryModInt64() = default;
template <typename T, typename std::enable_if<std::is_arithmetic<T>::value || std::is_same<T, __int128>::value || std::is_same<T, __uint128_t>::value, int>::type = 0>
constexpr MontgomeryModInt64(T v) : v_(reduce(mul(norm(v % i64(__mod)), r2))) {}
constexpr MontgomeryModInt64(const m64 &) = default;
constexpr u64 val() const { return reduce({0, v_}); }
template <typename T, typename std::enable_if<std::is_arithmetic<T>::value || std::is_same<T, __int128>::value || std::is_same<T, __uint128_t>::value, int>::type = 0>
explicit constexpr operator T() const { return T(val()); }
constexpr m64 operator-() const {
m64 res;
res.v_ = (__mod & -(v_ != 0)) - v_;
return res;
}
constexpr m64 inv_exgcd() const {
i64 x1 = 1, x3 = 0, a = val(), b = __mod;
while (b != 0) {
i64 q = a / b, x1_old = x1, a_old = a;
x1 = x3, x3 = x1_old - x3 * q, a = b, b = a_old - b * q;
}
return m64(x1);
}
constexpr m64 inv_fermat() const { return pow(__mod - 2); }
constexpr m64 inv() const { return s_isPrime ? inv_fermat() : inv_exgcd(); }
constexpr m64 &operator=(const m64 &) = default;
constexpr m64 &operator+=(const m64 &rhs) {
v_ += rhs.v_ - __mod;
v_ += __mod & -(v_ >> 63);
return *this;
}
constexpr m64 &operator-=(const m64 &rhs) {
v_ -= rhs.v_;
v_ += __mod & -(v_ >> 63);
return *this;
}
constexpr m64 &operator*=(const m64 &rhs) {
v_ = reduce(mul(v_, rhs.v_));
return *this;
}
constexpr m64 &operator/=(const m64 &rhs) { return operator*=(rhs.inv()); }
friend constexpr m64 operator+(const m64 &lhs, const m64 &rhs) { return m64(lhs) += rhs; }
friend constexpr m64 operator-(const m64 &lhs, const m64 &rhs) { return m64(lhs) -= rhs; }
friend constexpr m64 operator*(const m64 &lhs, const m64 &rhs) { return m64(lhs) *= rhs; }
friend constexpr m64 operator/(const m64 &lhs, const m64 &rhs) { return m64(lhs) /= rhs; }
friend constexpr bool operator==(const m64 &lhs, const m64 &rhs) { return lhs.v_ == rhs.v_; }
friend constexpr bool operator!=(const m64 &lhs, const m64 &rhs) { return lhs.v_ != rhs.v_; }
template <typename _Istream>
friend _Istream &operator>>(_Istream &is, m64 &rhs) {
i64 x;
is >> x;
rhs = m64(x);
return is;
}
template <typename _Ostream>
friend _Ostream &operator<<(_Ostream &os, const m64 &rhs) { return os << rhs.val(); }
constexpr m64 pow(u64 y) const {
m64 res(1), x(*this);
for (; y != 0; y >>= 1, x *= x) if (y & 1) res *= x;
return res;
}
private:
static constexpr std::pair<u64, u64> mul(u64 x, u64 y) {
#ifdef __GNUC__
unsigned __int128 res = (unsigned __int128)x * y;
return {u64(res >> 64), u64(res)};
#else
u64 a = x >> 32, b = u32(x), c = y >> 32, d = u32(y), ad = a * d, bc = b * c;
return {a * c + (ad >> 32) + (bc >> 32) + (((ad & ~UINT32_C(0)) + (bc & ~UINT32_C(0)) + (b * d >> 32)) >> 32), x * y};
#endif
}
static constexpr u64 mulh(u64 x, u64 y) {
#ifdef __GNUC__
return u64((unsigned __int128)x * y >> 64);
#else
u64 a = x >> 32, b = u32(x), c = y >> 32, d = u32(y), ad = a * d, bc = b * c;
return a * c + (ad >> 32) + (bc >> 32) + (((ad & ~UINT32_C(0)) + (bc & ~UINT32_C(0)) + (b * d >> 32)) >> 32);
#endif
}
static constexpr u64 get_r() {
u64 two = 2, iv = __mod * (two - __mod * __mod);
iv *= two - __mod * iv;
iv *= two - __mod * iv;
iv *= two - __mod * iv;
return iv * (two - __mod * iv);
}
static constexpr u64 get_r2() {
u64 iv = -u64(__mod) % __mod;
for (int i = 0; i != 64; ++i) ((iv <<= 1) >= __mod) && (iv -= __mod);
return iv;
}
static constexpr u64 reduce(const std::pair<u64, u64> &x) {
u64 res = x.first - mulh(x.second * r, __mod);
return res + (__mod & -(res >> 63));
}
static constexpr u64 norm(i64 x) { return x + (__mod & -(x < 0)); }
u64 v_;
public:
static constexpr u64 r = get_r(); // 13499267949257065399
static constexpr u64 r2 = get_r2(); // 279632277
static_assert((__mod & 1) == 1, "mod % 2 == 0\n");
static_assert(r * __mod == 1, "???\n");
static_assert((__mod & (1ULL << 63)) == 0, "mod >= (1ULL << 63)\n");
static_assert(__mod != 1, "mod == 1\n");
};模拟进出数域的过程。
现在来看右移 位如何实现。蒙哥马利模乘本质上存的是模数在 下的逆元。我们同样也可以求出在 下的逆元。
咕咕咕。