internal/internal_math.hpp
lib/internal/internal_math.hpp
概要
Fast modular multiplication by barrett reduction Reference: https://en.wikipedia.org/wiki/Barrett_reduction NOTE: reconsider after Ice Lake NOTE: 法は 32bit (unsigned int) 前提。64bit 法には montgomery を使う
インクルード
#include "internal/internal_math.hpp"
内部実装
直接使うことは想定していないが、公開 API の基底として現れる。
| 名前 | 説明 |
|---|---|
barrett |
Fast modular multiplication by barrett reduction Reference: https://en.wikipedia.org/wiki/Barrett_reduction NOTE: reconsider after Ice Lake NOTE: 法は 32bit (unsigned int) 前提。64bit 法には montgomery を使う |
montgomery |
Montgomery modular multiplication |
is_SPRP64 |
given $2 \le n, a < 2^{64}$, $a$ prime, check whether $n$ is $a$-SPRP without 2,3,5,13,19,73,193,407521,299210837 |
primitive_root_constexpr |
Compile time primitive root |
このヘッダを使うライブラリ(19)
-
ベル数を $B_0$ から $B_n$ まで列挙する
lib/combinatorics/bell.hpp -
二項係数・階乗・順列・多項係数・重複組合せ
lib/combinatorics/combinatorics.hpp -
複数の二項係数部分和 $\sum_{i=0}^{k}\binom{n}{i}$ をまとめて求める
lib/combinatorics/offline_binomial_sum.hpp -
分割数を $p(0)$ から $p(n)$ まで列挙する
lib/combinatorics/partition_function.hpp -
第1種スターリング数(符号付き)s(n, 0), s(n, 1), ..., s(n, n) を求める(行)
lib/combinatorics/stirling.hpp -
第2種スターリング数・ベル数
lib/combinatorics/stirling_bell.hpp -
形式的冪級数 (FPS)
lib/convolution/formal_power_series.hpp -
Garner法で連立合同式の解を指定した法で復元する
lib/convolution/garner.hpp -
NTT・畳み込み (convolution)
lib/convolution/ntt.hpp -
Montgomery + AVX2 による NTT 畳み込み (NTT-friendly な 32bit 素数 mod 専用)
lib/convolution/ntt_avx2.hpp -
internal/internal_fft.hpp
lib/internal/internal_fft.hpp -
internal/internal_fft_avx2.hpp
lib/internal/internal_fft_avx2.hpp -
internal/internal_fps_avx2.hpp
lib/internal/internal_fps_avx2.hpp -
きたまさ法(任意可換環版、$O(k^2 \log n)$)
lib/linalg/linear_recurrence.hpp -
多倍長整数
lib/number_theory/bigint.hpp -
素因数分解
lib/number_theory/factorize.hpp -
modint (static_modint / dynamic_modint)
lib/number_theory/modint.hpp -
素数列挙・判定
lib/number_theory/prime_number.hpp -
素数法上で `x` の平方根を 1 つ返す
lib/number_theory/root_mod.hpp
検証プログラム(78)
-
test/aoj/alds1/kitamasa.test.cpp
test/aoj/alds1/kitamasa.test.cpp -
test/aoj/challenge/0009.test.cpp
test/aoj/challenge/0009.test.cpp -
test/aoj/challenge/0264.test.cpp
test/aoj/challenge/0264.test.cpp -
test/aoj/challenge/1276.test.cpp
test/aoj/challenge/1276.test.cpp -
test/aoj/challenge/2863.test.cpp
test/aoj/challenge/2863.test.cpp -
test/aoj/course/ALDS1_1_C.test.cpp
test/aoj/course/ALDS1_1_C.test.cpp -
test/aoj/dpl/bell.test.cpp
test/aoj/dpl/bell.test.cpp -
test/aoj/dpl/stirling.test.cpp
test/aoj/dpl/stirling.test.cpp -
test/aoj/ntl/addition.test.cpp
test/aoj/ntl/addition.test.cpp -
test/aoj/ntl/difference.test.cpp
test/aoj/ntl/difference.test.cpp -
test/aoj/ntl/division.test.cpp
test/aoj/ntl/division.test.cpp -
test/aoj/ntl/multiplication.test.cpp
test/aoj/ntl/multiplication.test.cpp -
test/aoj/ntl/multiplication2.test.cpp
test/aoj/ntl/multiplication2.test.cpp -
test/aoj/ntl/power.test.cpp
test/aoj/ntl/power.test.cpp -
test/aoj/ntl/prime_factorize.test.cpp
test/aoj/ntl/prime_factorize.test.cpp -
test/aoj/ntl/remainder.test.cpp
test/aoj/ntl/remainder.test.cpp -
test/yosupo/big_integer/addition_of_big_integers.test.cpp
test/yosupo/big_integer/addition_of_big_integers.test.cpp -
test/yosupo/big_integer/division_of_big_integers.test.cpp
test/yosupo/big_integer/division_of_big_integers.test.cpp -
test/yosupo/big_integer/multiplication_of_big_integers.test.cpp
test/yosupo/big_integer/multiplication_of_big_integers.test.cpp -
test/yosupo/convolution/bitwise_and_convolution.test.cpp
test/yosupo/convolution/bitwise_and_convolution.test.cpp -
test/yosupo/convolution/bitwise_xor_convolution.test.cpp
test/yosupo/convolution/bitwise_xor_convolution.test.cpp -
test/yosupo/convolution/convolution.test.cpp
test/yosupo/convolution/convolution.test.cpp -
test/yosupo/convolution/convolution_mod.test.cpp
test/yosupo/convolution/convolution_mod.test.cpp -
test/yosupo/convolution/gcd_convolution.test.cpp
test/yosupo/convolution/gcd_convolution.test.cpp -
test/yosupo/convolution/lcm_convolution.test.cpp
test/yosupo/convolution/lcm_convolution.test.cpp -
test/yosupo/data_structure/dynamic_sequence_range_affine_range_sum.test.cpp
test/yosupo/data_structure/dynamic_sequence_range_affine_range_sum.test.cpp -
test/yosupo/data_structure/persistent_range_affine_range_sum.test.cpp
test/yosupo/data_structure/persistent_range_affine_range_sum.test.cpp -
test/yosupo/data_structure/point_set_range_composite.test.cpp
test/yosupo/data_structure/point_set_range_composite.test.cpp -
test/yosupo/data_structure/point_set_range_composite_large_array.test.cpp
test/yosupo/data_structure/point_set_range_composite_large_array.test.cpp -
test/yosupo/data_structure/queue_operate_all_composite.test.cpp
test/yosupo/data_structure/queue_operate_all_composite.test.cpp -
test/yosupo/data_structure/range_affine_range_sum.test.cpp
test/yosupo/data_structure/range_affine_range_sum.test.cpp -
test/yosupo/data_structure/range_affine_range_sum_large_array.test.cpp
test/yosupo/data_structure/range_affine_range_sum_large_array.test.cpp -
test/yosupo/data_structure/unionfind_with_potential.test.cpp
test/yosupo/data_structure/unionfind_with_potential.test.cpp -
test/yosupo/data_structure/unionfind_with_potential_non_commutative_group.test.cpp
test/yosupo/data_structure/unionfind_with_potential_non_commutative_group.test.cpp -
test/yosupo/enumerative_conbinatorics/bell_number.test.cpp
test/yosupo/enumerative_conbinatorics/bell_number.test.cpp -
test/yosupo/enumerative_conbinatorics/partition_function.test.cpp
test/yosupo/enumerative_conbinatorics/partition_function.test.cpp -
test/yosupo/enumerative_conbinatorics/stirling_number_of_the_first_kind.test.cpp
test/yosupo/enumerative_conbinatorics/stirling_number_of_the_first_kind.test.cpp -
test/yosupo/enumerative_conbinatorics/stirling_number_of_the_first_kind_fixed_k.test.cpp
test/yosupo/enumerative_conbinatorics/stirling_number_of_the_first_kind_fixed_k.test.cpp -
test/yosupo/enumerative_conbinatorics/stirling_number_of_the_second_kind.test.cpp
test/yosupo/enumerative_conbinatorics/stirling_number_of_the_second_kind.test.cpp -
test/yosupo/enumerative_conbinatorics/stirling_number_of_the_second_kind_fixed_k.test.cpp
test/yosupo/enumerative_conbinatorics/stirling_number_of_the_second_kind_fixed_k.test.cpp -
test/yosupo/math/bostan_mori.test.cpp
test/yosupo/math/bostan_mori.test.cpp -
test/yosupo/math/find_linear_recurrence.test.cpp
test/yosupo/math/find_linear_recurrence.test.cpp -
test/yosupo/math/kth_root_mod.test.cpp
test/yosupo/math/kth_root_mod.test.cpp -
test/yosupo/math/number_of_subsequences.test.cpp
test/yosupo/math/number_of_subsequences.test.cpp -
test/yosupo/math/sqrt_mod.test.cpp
test/yosupo/math/sqrt_mod.test.cpp -
test/yosupo/matrix/determinant_of_matrix.test.cpp
test/yosupo/matrix/determinant_of_matrix.test.cpp -
test/yosupo/matrix/matrix_product.test.cpp
test/yosupo/matrix/matrix_product.test.cpp -
test/yosupo/number_theory/discrete_logarithm_mod.test.cpp
test/yosupo/number_theory/discrete_logarithm_mod.test.cpp -
test/yosupo/number_theory/factorize.test.cpp
test/yosupo/number_theory/factorize.test.cpp -
test/yosupo/number_theory/primitive_root.test.cpp
test/yosupo/number_theory/primitive_root.test.cpp -
test/yosupo/number_theory/sum_of_multiplicative_function.test.cpp
test/yosupo/number_theory/sum_of_multiplicative_function.test.cpp -
test/yosupo/number_theory/sum_of_multiplicative_function_large.test.cpp
test/yosupo/number_theory/sum_of_multiplicative_function_large.test.cpp -
test/yosupo/number_theory/sum_of_totient_function.test.cpp
test/yosupo/number_theory/sum_of_totient_function.test.cpp -
test/yosupo/polynomial/composition_of_formal_power_series.test.cpp
test/yosupo/polynomial/composition_of_formal_power_series.test.cpp -
test/yosupo/polynomial/composition_of_formal_power_series_large.test.cpp
test/yosupo/polynomial/composition_of_formal_power_series_large.test.cpp -
test/yosupo/polynomial/compositional_inverse_of_formal_power_series.test.cpp
test/yosupo/polynomial/compositional_inverse_of_formal_power_series.test.cpp -
test/yosupo/polynomial/compositional_inverse_of_formal_power_series_large.test.cpp
test/yosupo/polynomial/compositional_inverse_of_formal_power_series_large.test.cpp -
test/yosupo/polynomial/division_of_polynomials.test.cpp
test/yosupo/polynomial/division_of_polynomials.test.cpp -
test/yosupo/polynomial/exp_of_formal_power_series.test.cpp
test/yosupo/polynomial/exp_of_formal_power_series.test.cpp -
test/yosupo/polynomial/inv_of_formal_power_series.test.cpp
test/yosupo/polynomial/inv_of_formal_power_series.test.cpp -
test/yosupo/polynomial/inv_of_polynomials.test.cpp
test/yosupo/polynomial/inv_of_polynomials.test.cpp -
test/yosupo/polynomial/log_of_formal_power_series.test.cpp
test/yosupo/polynomial/log_of_formal_power_series.test.cpp -
test/yosupo/polynomial/multipoint_evaluation.test.cpp
test/yosupo/polynomial/multipoint_evaluation.test.cpp -
test/yosupo/polynomial/polynomial_interpolation.test.cpp
test/yosupo/polynomial/polynomial_interpolation.test.cpp -
test/yosupo/polynomial/polynomial_taylor_shift.test.cpp
test/yosupo/polynomial/polynomial_taylor_shift.test.cpp -
test/yosupo/polynomial/pow_of_formal_power_series.test.cpp
test/yosupo/polynomial/pow_of_formal_power_series.test.cpp -
test/yosupo/polynomial/product_of_polynomial_sequence.test.cpp
test/yosupo/polynomial/product_of_polynomial_sequence.test.cpp -
test/yosupo/polynomial/shift_of_sampling_points_of_polynomial.test.cpp
test/yosupo/polynomial/shift_of_sampling_points_of_polynomial.test.cpp -
test/yosupo/polynomial/sqrt_of_formal_power_series.test.cpp
test/yosupo/polynomial/sqrt_of_formal_power_series.test.cpp -
test/yosupo/set_power_series/subset_convolution.test.cpp
test/yosupo/set_power_series/subset_convolution.test.cpp -
test/yosupo/tree/point_set_tree_path_composite_sum.test.cpp
test/yosupo/tree/point_set_tree_path_composite_sum.test.cpp -
test/yosupo/tree/point_set_tree_path_composite_sum_fixed_root.test.cpp
test/yosupo/tree/point_set_tree_path_composite_sum_fixed_root.test.cpp -
test/yosupo/tree/tree_path_composite_sum.test.cpp
test/yosupo/tree/tree_path_composite_sum.test.cpp -
test/yosupo/tree/vertex_set_path_composite.test.cpp
test/yosupo/tree/vertex_set_path_composite.test.cpp -
test/yukicoder/0551.test.cpp
test/yukicoder/0551.test.cpp -
test/yukicoder/0843.test.cpp
test/yukicoder/0843.test.cpp -
test/yukicoder/1611.test.cpp
test/yukicoder/1611.test.cpp -
test/yukicoder/2206.test.cpp
test/yukicoder/2206.test.cpp
実装
#pragma once
#include <cstdint>
#include <limits>
#include <utility>
namespace internal {
/// @param m `1 <= m`
/// @return x mod m
constexpr std::int64_t safe_mod(std::int64_t x, std::int64_t m) {
x %= m;
if (x < 0) x += m;
return x;
}
/// Fast modular multiplication by barrett reduction
/// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
/// NOTE: reconsider after Ice Lake
/// NOTE: 法は 32bit (`unsigned int`) 前提。64bit 法には montgomery を使う
struct barrett {
unsigned int _m;
std::uint64_t im;
/// @param m `1 <= m`
explicit constexpr barrett(unsigned int m) : _m(m), im((std::uint64_t)(-1) / m + 1) {}
/// @return m
constexpr unsigned int umod() const { return _m; }
/// @param a `0 <= a < m`
/// @param b `0 <= b < m`
/// @return `a * b % m`
constexpr unsigned int mul(unsigned int a, unsigned int b) const {
std::uint64_t z = a;
z *= b;
std::uint64_t x = (std::uint64_t)(((__uint128_t)(z)*im) >> 64);
std::uint64_t y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};
/// @brief Montgomery modular multiplication
/// @see https://rsk0315.hatenablog.com/entry/2022/11/27/060616
struct montgomery {
/// @param m `1 <= m`
explicit constexpr montgomery(std::uint64_t m)
: _m(m), im(m), r((__uint128_t(1) << 64) % m), r2(-__uint128_t(m) % m) {
for (int i = 0; i < 5; ++i) im = im * (2 - _m * im);
im = -im;
}
/// @return m
constexpr std::uint64_t umod() const { return _m; }
/// @param a `0 <= a < m`
/// @param b `0 <= b < m`
/// @return `a * b % m`
constexpr std::uint64_t mul(std::uint64_t a, std::uint64_t b) const {
return mr((__uint128_t)a * mr((__uint128_t)b * r2));
}
constexpr std::uint64_t exp(std::uint64_t a, std::uint64_t b) const {
std::uint64_t res = 1, p = mr((__uint128_t)a * r2);
while (b) {
if (b & 1) res = mr((__uint128_t)res * p);
p = mr((__uint128_t)p * p);
b >>= 1;
}
return res;
}
constexpr bool same_pow(std::uint64_t x, int s, std::uint64_t n) const {
x = mr((__uint128_t)x * r2), n = mr((__uint128_t)n * r2);
for (int r = 0; r < s; r++) {
if (x == n) return true;
x = mr((__uint128_t)x * x);
}
return false;
}
private:
std::uint64_t _m, im, r, r2;
// Montgomery 簡約。入力 t < _m * 2^64 を t * 2^-64 mod _m に写す。
// _m >= 2^63 のとき t + (lo * _m) が 2^128 を超え得るので、
// 128bit 加算のキャリーを検出して上位へ反映する(これを欠くと大 mod で誤る)。
constexpr std::uint64_t mr(std::uint64_t x) const {
__uint128_t sum = __uint128_t(x * im) * _m + x;
std::uint64_t res = sum >> 64;
if (sum < (__uint128_t)x) res -= _m;
return res >= _m ? res - _m : res;
}
constexpr std::uint64_t mr(__uint128_t x) const {
__uint128_t sum = __uint128_t(std::uint64_t(x) * im) * _m + x;
std::uint64_t res = sum >> 64;
if (sum < x) res -= _m;
return res >= _m ? res - _m : res;
}
constexpr std::uint64_t mr(std::uint64_t a, std::uint64_t b) const {
__uint128_t t = (__uint128_t)a * b;
std::uint64_t x = t >> 64, y = t;
x = mr((__uint128_t)x * r), y = mr(y);
return x + y >= _m ? x + y - _m : x + y;
}
};
constexpr bool is_SPRP32(std::uint32_t n, std::uint32_t a) {
std::uint32_t d = n - 1, s = 0;
while ((d & 1) == 0) ++s, d >>= 1;
std::uint64_t cur = 1, pw = d;
while (pw) {
if (pw & 1) cur = (cur * a) % n;
a = (std::uint64_t)a * a % n;
pw >>= 1;
}
if (cur == 1) return true;
for (std::uint32_t r = 0; r < s; r++) {
if (cur == n - 1) return true;
cur = cur * cur % n;
}
return false;
}
/// given $2 \le n, a < 2^{64}$, $a$ prime, check whether $n$ is $a$-SPRP
/// without 2,3,5,13,19,73,193,407521,299210837
constexpr bool is_SPRP64(const montgomery &m, std::uint64_t a) {
auto n = m.umod();
std::uint64_t d = n - 1;
int s = 0;
while ((d & 1) == 0) ++s, d >>= 1;
std::uint64_t cur = m.exp(a, d);
if (cur == 1) return true;
return m.same_pow(cur, s, n - 1);
}
constexpr bool is_prime_constexpr(std::uint32_t x) {
if (x == 2 || x == 3 || x == 5 || x == 7) return true;
if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0 || x % 7 == 0) return false;
if (x < 121) return (x > 1);
std::uint64_t h = x;
h = ((h >> 16) ^ h) * 0x45d9f3b;
h = ((h >> 16) ^ h) * 0x45d9f3b;
h = ((h >> 16) ^ h) & 255;
constexpr std::uint16_t bases[] = {
15591, 2018, 166, 7429, 8064, 16045, 10503, 4399, 1949, 1295, 2776, 3620, 560, 3128, 5212, 2657,
2300, 2021, 4652, 1471, 9336, 4018, 2398, 20462, 10277, 8028, 2213, 6219, 620, 3763, 4852, 5012,
3185, 1333, 6227, 5298, 1074, 2391, 5113, 7061, 803, 1269, 3875, 422, 751, 580, 4729, 10239,
746, 2951, 556, 2206, 3778, 481, 1522, 3476, 481, 2487, 3266, 5633, 488, 3373, 6441, 3344,
17, 15105, 1490, 4154, 2036, 1882, 1813, 467, 3307, 14042, 6371, 658, 1005, 903, 737, 1887,
7447, 1888, 2848, 1784, 7559, 3400, 951, 13969, 4304, 177, 41, 19875, 3110, 13221, 8726, 571,
7043, 6943, 1199, 352, 6435, 165, 1169, 3315, 978, 233, 3003, 2562, 2994, 10587, 10030, 2377,
1902, 5354, 4447, 1555, 263, 27027, 2283, 305, 669, 1912, 601, 6186, 429, 1930, 14873, 1784,
1661, 524, 3577, 236, 2360, 6146, 2850, 55637, 1753, 4178, 8466, 222, 2579, 2743, 2031, 2226,
2276, 374, 2132, 813, 23788, 1610, 4422, 5159, 1725, 3597, 3366, 14336, 579, 165, 1375, 10018,
12616, 9816, 1371, 536, 1867, 10864, 857, 2206, 5788, 434, 8085, 17618, 727, 3639, 1595, 4944,
2129, 2029, 8195, 8344, 6232, 9183, 8126, 1870, 3296, 7455, 8947, 25017, 541, 19115, 368, 566,
5674, 411, 522, 1027, 8215, 2050, 6544, 10049, 614, 774, 2333, 3007, 35201, 4706, 1152, 1785,
1028, 1540, 3743, 493, 4474, 2521, 26845, 8354, 864, 18915, 5465, 2447, 42, 4511, 1660, 166,
1249, 6259, 2553, 304, 272, 7286, 73, 6554, 899, 2816, 5197, 13330, 7054, 2818, 3199, 811,
922, 350, 7514, 4452, 3449, 2663, 4708, 418, 1621, 1171, 3471, 88, 11345, 412, 1559, 194};
return is_SPRP32(x, bases[h]);
}
constexpr bool is_prime_constexpr(std::uint64_t x) {
if (x <= std::numeric_limits<std::uint32_t>::max()) return is_prime_constexpr((std::uint32_t)x);
if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0 || x % 7 == 0) return false;
montgomery m(x);
constexpr std::uint64_t bases[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
for (auto a : bases) {
if (!is_SPRP64(m, a)) return false;
}
return true;
}
constexpr bool is_prime_constexpr(std::int64_t x) {
if (x < 0) return false;
return is_prime_constexpr(std::uint64_t(x));
}
template <int n>
constexpr bool is_prime = is_prime_constexpr((std::uint32_t)n);
/// @param n `0 <= n`
/// @param m `1 <= m`
/// @return `(x ** n) % m`
constexpr std::int64_t pow_mod_constexpr(std::int64_t x, std::int64_t n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
std::uint64_t r = 1;
std::uint64_t y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
/// @param b `1 <= b`
/// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), `0 <= x < b/g`
constexpr std::pair<std::int64_t, std::int64_t> inv_gcd(std::int64_t a, std::int64_t b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
std::int64_t s = b, t = a;
std::int64_t m0 = 0, m1 = 1;
while (t) {
std::int64_t u = s / t;
s -= t * u;
m0 -= m1 * u;
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0) m0 += b / s;
return {s, m0};
}
/// Compile time primitive root
/// @param m must be prime
/// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (std::int64_t)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) { x /= i; }
}
}
if (x > 1) { divs[cnt++] = x; }
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m>
constexpr int primitive_root = primitive_root_constexpr(m);
} // namespace internal
bundled
#line 2 "lib/internal/internal_math.hpp"
#include <cstdint>
#include <limits>
#include <utility>
namespace internal {
/// @param m `1 <= m`
/// @return x mod m
constexpr std::int64_t safe_mod(std::int64_t x, std::int64_t m) {
x %= m;
if (x < 0) x += m;
return x;
}
/// Fast modular multiplication by barrett reduction
/// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
/// NOTE: reconsider after Ice Lake
/// NOTE: 法は 32bit (`unsigned int`) 前提。64bit 法には montgomery を使う
struct barrett {
unsigned int _m;
std::uint64_t im;
/// @param m `1 <= m`
explicit constexpr barrett(unsigned int m) : _m(m), im((std::uint64_t)(-1) / m + 1) {}
/// @return m
constexpr unsigned int umod() const { return _m; }
/// @param a `0 <= a < m`
/// @param b `0 <= b < m`
/// @return `a * b % m`
constexpr unsigned int mul(unsigned int a, unsigned int b) const {
std::uint64_t z = a;
z *= b;
std::uint64_t x = (std::uint64_t)(((__uint128_t)(z)*im) >> 64);
std::uint64_t y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};
/// @brief Montgomery modular multiplication
/// @see https://rsk0315.hatenablog.com/entry/2022/11/27/060616
struct montgomery {
/// @param m `1 <= m`
explicit constexpr montgomery(std::uint64_t m)
: _m(m), im(m), r((__uint128_t(1) << 64) % m), r2(-__uint128_t(m) % m) {
for (int i = 0; i < 5; ++i) im = im * (2 - _m * im);
im = -im;
}
/// @return m
constexpr std::uint64_t umod() const { return _m; }
/// @param a `0 <= a < m`
/// @param b `0 <= b < m`
/// @return `a * b % m`
constexpr std::uint64_t mul(std::uint64_t a, std::uint64_t b) const {
return mr((__uint128_t)a * mr((__uint128_t)b * r2));
}
constexpr std::uint64_t exp(std::uint64_t a, std::uint64_t b) const {
std::uint64_t res = 1, p = mr((__uint128_t)a * r2);
while (b) {
if (b & 1) res = mr((__uint128_t)res * p);
p = mr((__uint128_t)p * p);
b >>= 1;
}
return res;
}
constexpr bool same_pow(std::uint64_t x, int s, std::uint64_t n) const {
x = mr((__uint128_t)x * r2), n = mr((__uint128_t)n * r2);
for (int r = 0; r < s; r++) {
if (x == n) return true;
x = mr((__uint128_t)x * x);
}
return false;
}
private:
std::uint64_t _m, im, r, r2;
// Montgomery 簡約。入力 t < _m * 2^64 を t * 2^-64 mod _m に写す。
// _m >= 2^63 のとき t + (lo * _m) が 2^128 を超え得るので、
// 128bit 加算のキャリーを検出して上位へ反映する(これを欠くと大 mod で誤る)。
constexpr std::uint64_t mr(std::uint64_t x) const {
__uint128_t sum = __uint128_t(x * im) * _m + x;
std::uint64_t res = sum >> 64;
if (sum < (__uint128_t)x) res -= _m;
return res >= _m ? res - _m : res;
}
constexpr std::uint64_t mr(__uint128_t x) const {
__uint128_t sum = __uint128_t(std::uint64_t(x) * im) * _m + x;
std::uint64_t res = sum >> 64;
if (sum < x) res -= _m;
return res >= _m ? res - _m : res;
}
constexpr std::uint64_t mr(std::uint64_t a, std::uint64_t b) const {
__uint128_t t = (__uint128_t)a * b;
std::uint64_t x = t >> 64, y = t;
x = mr((__uint128_t)x * r), y = mr(y);
return x + y >= _m ? x + y - _m : x + y;
}
};
constexpr bool is_SPRP32(std::uint32_t n, std::uint32_t a) {
std::uint32_t d = n - 1, s = 0;
while ((d & 1) == 0) ++s, d >>= 1;
std::uint64_t cur = 1, pw = d;
while (pw) {
if (pw & 1) cur = (cur * a) % n;
a = (std::uint64_t)a * a % n;
pw >>= 1;
}
if (cur == 1) return true;
for (std::uint32_t r = 0; r < s; r++) {
if (cur == n - 1) return true;
cur = cur * cur % n;
}
return false;
}
/// given $2 \le n, a < 2^{64}$, $a$ prime, check whether $n$ is $a$-SPRP
/// without 2,3,5,13,19,73,193,407521,299210837
constexpr bool is_SPRP64(const montgomery &m, std::uint64_t a) {
auto n = m.umod();
std::uint64_t d = n - 1;
int s = 0;
while ((d & 1) == 0) ++s, d >>= 1;
std::uint64_t cur = m.exp(a, d);
if (cur == 1) return true;
return m.same_pow(cur, s, n - 1);
}
constexpr bool is_prime_constexpr(std::uint32_t x) {
if (x == 2 || x == 3 || x == 5 || x == 7) return true;
if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0 || x % 7 == 0) return false;
if (x < 121) return (x > 1);
std::uint64_t h = x;
h = ((h >> 16) ^ h) * 0x45d9f3b;
h = ((h >> 16) ^ h) * 0x45d9f3b;
h = ((h >> 16) ^ h) & 255;
constexpr std::uint16_t bases[] = {
15591, 2018, 166, 7429, 8064, 16045, 10503, 4399, 1949, 1295, 2776, 3620, 560, 3128, 5212, 2657,
2300, 2021, 4652, 1471, 9336, 4018, 2398, 20462, 10277, 8028, 2213, 6219, 620, 3763, 4852, 5012,
3185, 1333, 6227, 5298, 1074, 2391, 5113, 7061, 803, 1269, 3875, 422, 751, 580, 4729, 10239,
746, 2951, 556, 2206, 3778, 481, 1522, 3476, 481, 2487, 3266, 5633, 488, 3373, 6441, 3344,
17, 15105, 1490, 4154, 2036, 1882, 1813, 467, 3307, 14042, 6371, 658, 1005, 903, 737, 1887,
7447, 1888, 2848, 1784, 7559, 3400, 951, 13969, 4304, 177, 41, 19875, 3110, 13221, 8726, 571,
7043, 6943, 1199, 352, 6435, 165, 1169, 3315, 978, 233, 3003, 2562, 2994, 10587, 10030, 2377,
1902, 5354, 4447, 1555, 263, 27027, 2283, 305, 669, 1912, 601, 6186, 429, 1930, 14873, 1784,
1661, 524, 3577, 236, 2360, 6146, 2850, 55637, 1753, 4178, 8466, 222, 2579, 2743, 2031, 2226,
2276, 374, 2132, 813, 23788, 1610, 4422, 5159, 1725, 3597, 3366, 14336, 579, 165, 1375, 10018,
12616, 9816, 1371, 536, 1867, 10864, 857, 2206, 5788, 434, 8085, 17618, 727, 3639, 1595, 4944,
2129, 2029, 8195, 8344, 6232, 9183, 8126, 1870, 3296, 7455, 8947, 25017, 541, 19115, 368, 566,
5674, 411, 522, 1027, 8215, 2050, 6544, 10049, 614, 774, 2333, 3007, 35201, 4706, 1152, 1785,
1028, 1540, 3743, 493, 4474, 2521, 26845, 8354, 864, 18915, 5465, 2447, 42, 4511, 1660, 166,
1249, 6259, 2553, 304, 272, 7286, 73, 6554, 899, 2816, 5197, 13330, 7054, 2818, 3199, 811,
922, 350, 7514, 4452, 3449, 2663, 4708, 418, 1621, 1171, 3471, 88, 11345, 412, 1559, 194};
return is_SPRP32(x, bases[h]);
}
constexpr bool is_prime_constexpr(std::uint64_t x) {
if (x <= std::numeric_limits<std::uint32_t>::max()) return is_prime_constexpr((std::uint32_t)x);
if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0 || x % 7 == 0) return false;
montgomery m(x);
constexpr std::uint64_t bases[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
for (auto a : bases) {
if (!is_SPRP64(m, a)) return false;
}
return true;
}
constexpr bool is_prime_constexpr(std::int64_t x) {
if (x < 0) return false;
return is_prime_constexpr(std::uint64_t(x));
}
template <int n>
constexpr bool is_prime = is_prime_constexpr((std::uint32_t)n);
/// @param n `0 <= n`
/// @param m `1 <= m`
/// @return `(x ** n) % m`
constexpr std::int64_t pow_mod_constexpr(std::int64_t x, std::int64_t n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
std::uint64_t r = 1;
std::uint64_t y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
/// @param b `1 <= b`
/// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), `0 <= x < b/g`
constexpr std::pair<std::int64_t, std::int64_t> inv_gcd(std::int64_t a, std::int64_t b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
std::int64_t s = b, t = a;
std::int64_t m0 = 0, m1 = 1;
while (t) {
std::int64_t u = s / t;
s -= t * u;
m0 -= m1 * u;
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0) m0 += b / s;
return {s, m0};
}
/// Compile time primitive root
/// @param m must be prime
/// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
if (m == 2) return 1;
if (m == 167772161) return 3;
if (m == 469762049) return 3;
if (m == 754974721) return 11;
if (m == 998244353) return 3;
int divs[20] = {};
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0) x /= 2;
for (int i = 3; (std::int64_t)(i)*i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) { x /= i; }
}
}
if (x > 1) { divs[cnt++] = x; }
for (int g = 2;; g++) {
bool ok = true;
for (int i = 0; i < cnt; i++) {
if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok) return g;
}
}
template <int m>
constexpr int primitive_root = primitive_root_constexpr(m);
} // namespace internal