This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <iostream>
#include <atcoder/modint>
using mint = atcoder::modint998244353;
#include "library/convolution/multi_variate_convolution_circular.hpp"
void test1() {
using namespace suisen;
std::vector<int> n { 2, 45, 73 };
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 6570
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 123109233);
std::iota(g.begin(), g.end(), 213082409);
std::vector<mint> h_expected = conv.convolution_naive(f, g);
std::vector<mint> h_actual = conv.convolution(f, g);
assert(h_expected == h_actual);
}
void test2() {
using namespace suisen;
std::vector<int> n { 2, 3, 2, 4, 3, 5 };
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 720
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 12038);
std::iota(g.begin(), g.end(), 4392);
std::vector<mint> h_expected = conv.convolution_naive(f, g);
std::vector<mint> h_actual = conv.convolution(f, g);
assert(h_expected == h_actual);
}
void test3() {
using namespace suisen;
std::vector<int> n {};
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 1
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 4);
std::iota(g.begin(), g.end(), 3);
std::vector<mint> h_expected = conv.convolution_naive(f, g);
std::vector<mint> h_actual = conv.convolution(f, g);
assert(h_expected == h_actual);
}
#include "library/util/timer.hpp"
void perf_test1() {
using namespace suisen;
std::vector<int> n(18, 2);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test2() {
using namespace suisen;
std::vector<int> n(11, 3);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 177147
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test3() {
using namespace suisen;
std::vector<int> n { 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 236196
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test4() {
using namespace suisen;
std::vector<int> n(9, 4);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test5() {
using namespace suisen;
std::vector<int> n(7, 6);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 279936
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test6() {
using namespace suisen;
std::vector<int> n(6, 8);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test7() {
using namespace suisen;
std::vector<int> n(5, 12);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 248832
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test8() {
using namespace suisen;
std::vector<int> n(4, 22);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 234256
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test9() {
using namespace suisen;
std::vector<int> n(3, 64);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test10() {
using namespace suisen;
std::vector<int> n(2, 512);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test11() {
using namespace suisen;
std::vector<int> n(1, 262144);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void test() {
test1();
test2();
test3();
perf_test1();
perf_test2();
perf_test3();
perf_test4();
perf_test5();
perf_test6();
perf_test7();
perf_test8();
perf_test9();
perf_test10();
perf_test11();
}
int main() {
test();
std::cout << "Hello World" << std::endl;
return 0;
}#line 1 "test/src/convolution/multi_variate_convolution_circular/dummy.test.cpp"
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
#include <iostream>
#include <atcoder/modint>
using mint = atcoder::modint998244353;
#line 1 "library/convolution/multi_variate_convolution_circular.hpp"
#line 5 "library/convolution/multi_variate_convolution_circular.hpp"
#line 1 "library/transform/chirp_z_transform.hpp"
#include <algorithm>
#include <vector>
#include <atcoder/convolution>
/**
* @brief chirp z-transform ($g _ k = f(a r^k)$)
*/
namespace suisen {
namespace internal {
const auto default_convolution = [](const auto& a, const auto& b) { return atcoder::convolution(a, b); };
template <typename T>
std::vector<T> chirp_z_transform_naive(const std::vector<T> &f, T a, T r, int m) {
const int n = f.size();
std::vector<T> g(m);
T pow_r = 1;
for (int k = 0; k < m; ++k) {
T ark = a * pow_r, pow_ark = 1;
for (int i = 0; i < n; ++i) {
g[k] += f[i] * pow_ark;
pow_ark *= ark;
}
pow_r *= r;
}
return g;
}
} // namespace internal
/**
* @brief Calculates f(ar^k) for k=0,...,m-1 in O(M(n+m-1)+n+m) time
*/
template <typename T, typename Convolution>
std::vector<T> chirp_z_transform(std::vector<T> f, T a, T r, int m, Convolution&& convolution = internal::default_convolution) {
const int n = f.size();
std::vector<T> g(m);
if (n == 0 or m == 0) return g;
T pow_a = 1;
for (int i = 0; i < n; ++i, pow_a *= a) f[i] *= pow_a;
if (r == 0) {
for (int i = 0; i < n; ++i) g[0] += f[i];
for (int k = 1; k < m; ++k) g[k] += f[0];
return g;
}
if (n < 60) return internal::chirp_z_transform_naive(f, a, r, m);
const T r_inv = r.inv();
const int l = n + m - 1;
std::vector<T> pow_r_tri(l), pow_r_tri_inv(l);
pow_r_tri[0] = pow_r_tri_inv[0] = 1;
T pow_r = 1, pow_r_inv = 1;
for (int i = 1; i < l; ++i, pow_r *= r, pow_r_inv *= r_inv) {
pow_r_tri[i] = pow_r_tri[i - 1] * pow_r;
pow_r_tri_inv[i] = pow_r_tri_inv[i - 1] * pow_r_inv;
}
std::vector<T> p(n), q(l);
for (int i = 0; i < n; ++i) p[i] = f[i] * pow_r_tri_inv[i];
for (int i = 0; i < l; ++i) q[i] = pow_r_tri[i];
std::reverse(p.begin(), p.end());
std::vector<T> pq = convolution(p, q);
for (int k = 0; k < m; ++k) {
g[k] = pow_r_tri_inv[k] * pq[n - 1 + k];
}
return g;
}
} // namespace suisen
#line 1 "library/convolution/arbitrary_mod_convolution.hpp"
#line 6 "library/convolution/arbitrary_mod_convolution.hpp"
#line 1 "library/convolution/convolution_naive.hpp"
#line 5 "library/convolution/convolution_naive.hpp"
namespace suisen::internal {
template <typename T, typename R = T>
std::vector<R> convolution_naive(const std::vector<T>& a, const std::vector<T>& b) {
const int n = a.size(), m = b.size();
std::vector<R> c(n + m - 1);
if (n < m) {
for (int j = 0; j < m; j++) for (int i = 0; i < n; i++) c[i + j] += R(a[i]) * b[j];
} else {
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) c[i + j] += R(a[i]) * b[j];
}
return c;
}
} // namespace suisen
#line 8 "library/convolution/arbitrary_mod_convolution.hpp"
namespace suisen {
template <typename mint, atcoder::internal::is_modint_t<mint>* = nullptr>
std::vector<mint> arbitrary_mod_convolution(const std::vector<mint>& a, const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
if constexpr (atcoder::internal::is_static_modint<mint>::value) {
if constexpr (not (mint::mod() & 63)) {
int maxz = 1;
while (not ((mint::mod() - 1) & maxz)) maxz <<= 1;
int z = 1;
while (z < n + m - 1) z <<= 1;
if (z <= maxz) return atcoder::convolution<mint>(a, b);
}
}
if (n == 0 or m == 0) return {};
if (std::min(n, m) <= 120) return internal::convolution_naive(a, b);
static constexpr long long MOD1 = 754974721; // 2^24
static constexpr long long MOD2 = 167772161; // 2^25
static constexpr long long MOD3 = 469762049; // 2^26
static constexpr long long M1M2 = MOD1 * MOD2;
static constexpr long long INV_M1_MOD2 = atcoder::internal::inv_gcd(MOD1, MOD2).second;
static constexpr long long INV_M1M2_MOD3 = atcoder::internal::inv_gcd(M1M2, MOD3).second;
std::vector<int> a2(n), b2(m);
for (int i = 0; i < n; ++i) a2[i] = a[i].val();
for (int i = 0; i < m; ++i) b2[i] = b[i].val();
auto c1 = atcoder::convolution<MOD1>(a2, b2);
auto c2 = atcoder::convolution<MOD2>(a2, b2);
auto c3 = atcoder::convolution<MOD3>(a2, b2);
const long long m1m2 = mint(M1M2).val();
std::vector<mint> c(n + m - 1);
for (int i = 0; i < n + m - 1; ++i) {
// Garner's Algorithm
// X = x1 + x2 * m1 + x3 * m1 * m2
// x1 = c1[i], x2 = (c2[i] - x1) / m1 (mod m2), x3 = (c3[i] - x1 - x2 * m1) / m2 (mod m3)
long long x1 = c1[i];
long long x2 = (atcoder::static_modint<MOD2>(c2[i] - x1) * INV_M1_MOD2).val();
long long x3 = (atcoder::static_modint<MOD3>(c3[i] - x1 - x2 * MOD1) * INV_M1M2_MOD3).val();
c[i] = x1 + x2 * MOD1 + x3 * m1m2;
}
return c;
}
std::vector<__uint128_t> convolution_int(const std::vector<int> &a, const std::vector<int> &b) {
int n = int(a.size()), m = int(b.size());
auto check_nonnegative = [](int e) { return e >= 0; };
assert(std::all_of(a.begin(), a.end(), check_nonnegative));
assert(std::all_of(b.begin(), b.end(), check_nonnegative));
if (n == 0 or m == 0) return {};
if (std::min(n, m) <= 120) return internal::convolution_naive<int, __uint128_t>(a, b);
static constexpr long long MOD1 = 754974721; // 2^24
static constexpr long long MOD2 = 167772161; // 2^25
static constexpr long long MOD3 = 469762049; // 2^26
static constexpr long long M1M2 = MOD1 * MOD2;
static constexpr long long INV_M1_MOD2 = atcoder::internal::inv_gcd(MOD1, MOD2).second;
static constexpr long long INV_M1M2_MOD3 = atcoder::internal::inv_gcd(M1M2, MOD3).second;
auto c1 = atcoder::convolution<MOD1>(a, b);
auto c2 = atcoder::convolution<MOD2>(a, b);
auto c3 = atcoder::convolution<MOD3>(a, b);
std::vector<__uint128_t> c(n + m - 1);
for (int i = 0; i < n + m - 1; ++i) {
// Garner's Algorithm
// X = x1 + x2 * m1 + x3 * m1 * m2
// x1 = c1[i], x2 = (c2[i] - x1) / m1 (mod m2), x3 = (c3[i] - x1 - x2 * m1) / m2 (mod m3)
int x1 = c1[i];
int x2 = (atcoder::static_modint<MOD2>(c2[i] - x1) * INV_M1_MOD2).val();
int x3 = (atcoder::static_modint<MOD3>(c3[i] - x1 - x2 * MOD1) * INV_M1M2_MOD3).val();
c[i] = x1 + x2 * MOD1 + __uint128_t(x3) * M1M2;
}
return c;
}
} // namespace suisen
#line 8 "library/convolution/multi_variate_convolution_circular.hpp"
#line 1 "library/number/deterministic_miller_rabin.hpp"
#include <array>
#include <cassert>
#include <cstdint>
#include <iterator>
#include <tuple>
#include <type_traits>
#line 1 "library/number/montgomery.hpp"
#line 6 "library/number/montgomery.hpp"
#include <limits>
namespace suisen {
namespace internal::montgomery {
template <typename Int, typename DInt>
struct Montgomery {
private:
static constexpr uint32_t bits = std::numeric_limits<Int>::digits;
static constexpr Int mask = ~Int(0);
// R = 2**32 or 2**64
// 1. N is an odd number
// 2. N < R
// 3. gcd(N, R) = 1
// 4. R * R2 - N * N2 = 1
// 5. 0 < R2 < N
// 6. 0 < N2 < R
Int N, N2, R2;
// RR = R * R (mod N)
Int RR;
public:
constexpr Montgomery() = default;
explicit constexpr Montgomery(Int N) : N(N), N2(calcN2(N)), R2(calcR2(N, N2)), RR(calcRR(N)) {
assert(N & 1);
}
// @returns t * R (mod N)
constexpr Int make(Int t) const {
return reduce(static_cast<DInt>(t) * RR);
}
// @returns T * R^(-1) (mod N)
constexpr Int reduce(DInt T) const {
// 0 <= T < RN
// Note:
// 1. m = T * N2 (mod R)
// 2. 0 <= m < R
DInt m = modR(static_cast<DInt>(modR(T)) * N2);
// Note:
// T + m * N = T + T * N * N2 = T + T * (R * R2 - 1) = 0 (mod R)
// => (T + m * N) / R is an integer.
// => t * R = T + m * N = T (mod N)
// => t = T R^(-1) (mod N)
DInt t = divR(T + m * N);
// Note:
// 1. 0 <= T < RN
// 2. 0 <= mN < RN (because 0 <= m < R)
// => 0 <= T + mN < 2RN
// => 0 <= t < 2N
return t >= N ? t - N : t;
}
constexpr Int add(Int A, Int B) const {
return (A += B) >= N ? A - N : A;
}
constexpr Int sub(Int A, Int B) const {
return (A -= B) < 0 ? A + N : A;
}
constexpr Int mul(Int A, Int B) const {
return reduce(static_cast<DInt>(A) * B);
}
constexpr Int div(Int A, Int B) const {
return reduce(static_cast<DInt>(A) * inv(B));
}
constexpr Int inv(Int A) const; // TODO: Implement
constexpr Int pow(Int A, long long b) const {
Int P = make(1);
for (; b; b >>= 1) {
if (b & 1) P = mul(P, A);
A = mul(A, A);
}
return P;
}
private:
static constexpr Int divR(DInt t) { return t >> bits; }
static constexpr Int modR(DInt t) { return t & mask; }
static constexpr Int calcN2(Int N) {
// - N * N2 = 1 (mod R)
// N2 = -N^{-1} (mod R)
// calculates N^{-1} (mod R) by Newton's method
DInt invN = N; // = N^{-1} (mod 2^2)
for (uint32_t cur_bits = 2; cur_bits < bits; cur_bits *= 2) {
// loop invariant: invN = N^{-1} (mod 2^cur_bits)
// x = a^{-1} mod m => x(2-ax) = a^{-1} mod m^2 because:
// ax = 1 (mod m)
// => (ax-1)^2 = 0 (mod m^2)
// => 2ax - a^2x^2 = 1 (mod m^2)
// => a(x(2-ax)) = 1 (mod m^2)
invN = modR(invN * modR(2 - N * invN));
}
assert(modR(N * invN) == 1);
return modR(-invN);
}
static constexpr Int calcR2(Int N, Int N2) {
// R * R2 - N * N2 = 1
// => R2 = (1 + N * N2) / R
return divR(1 + static_cast<DInt>(N) * N2);
}
static constexpr Int calcRR(Int N) {
return -DInt(N) % N;
}
};
} // namespace internal::montgomery
using Montgomery32 = internal::montgomery::Montgomery<uint32_t, uint64_t>;
using Montgomery64 = internal::montgomery::Montgomery<uint64_t, __uint128_t>;
} // namespace suisen
#line 12 "library/number/deterministic_miller_rabin.hpp"
namespace suisen::miller_rabin {
namespace internal {
constexpr uint64_t THRESHOLD_1 = 341531ULL;
constexpr uint64_t BASE_1[]{ 9345883071009581737ULL };
constexpr uint64_t THRESHOLD_2 = 1050535501ULL;
constexpr uint64_t BASE_2[]{ 336781006125ULL, 9639812373923155ULL };
constexpr uint64_t THRESHOLD_3 = 350269456337ULL;
constexpr uint64_t BASE_3[]{ 4230279247111683200ULL, 14694767155120705706ULL, 16641139526367750375ULL };
constexpr uint64_t THRESHOLD_4 = 55245642489451ULL;
constexpr uint64_t BASE_4[]{ 2ULL, 141889084524735ULL, 1199124725622454117ULL, 11096072698276303650ULL };
constexpr uint64_t THRESHOLD_5 = 7999252175582851ULL;
constexpr uint64_t BASE_5[]{ 2ULL, 4130806001517ULL, 149795463772692060ULL, 186635894390467037ULL, 3967304179347715805ULL };
constexpr uint64_t THRESHOLD_6 = 585226005592931977ULL;
constexpr uint64_t BASE_6[]{ 2ULL, 123635709730000ULL, 9233062284813009ULL, 43835965440333360ULL, 761179012939631437ULL, 1263739024124850375ULL };
constexpr uint64_t BASE_7[]{ 2U, 325U, 9375U, 28178U, 450775U, 9780504U, 1795265022U };
template <auto BASE, std::size_t SIZE>
constexpr bool miller_rabin(uint64_t n) {
if (n == 2 or n == 3 or n == 5 or n == 7) return true;
if (n <= 1 or n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n % 7 == 0) return false;
if (n < 121) return true;
const uint32_t s = __builtin_ctzll(n - 1); // >= 1
const uint64_t d = (n - 1) >> s;
const Montgomery64 mg{ n };
const uint64_t one = mg.make(1), minus_one = mg.make(n - 1);
for (std::size_t i = 0; i < SIZE; ++i) {
uint64_t a = BASE[i] % n;
if (a == 0) continue;
uint64_t Y = mg.pow(mg.make(a), d);
if (Y == one) continue;
for (uint32_t r = 0;; ++r, Y = mg.mul(Y, Y)) {
// Y = a^(d 2^r)
if (Y == minus_one) break;
if (r == s - 1) return false;
}
}
return true;
}
}
template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
constexpr bool is_prime(T n) {
if constexpr (std::is_signed_v<T>) {
assert(n >= 0);
}
const std::make_unsigned_t<T> n_unsigned = n;
assert(n_unsigned <= std::numeric_limits<uint64_t>::max()); // n < 2^64
using namespace internal;
if (n_unsigned < THRESHOLD_1) return miller_rabin<BASE_1, 1>(n_unsigned);
if (n_unsigned < THRESHOLD_2) return miller_rabin<BASE_2, 2>(n_unsigned);
if (n_unsigned < THRESHOLD_3) return miller_rabin<BASE_3, 3>(n_unsigned);
if (n_unsigned < THRESHOLD_4) return miller_rabin<BASE_4, 4>(n_unsigned);
if (n_unsigned < THRESHOLD_5) return miller_rabin<BASE_5, 5>(n_unsigned);
if (n_unsigned < THRESHOLD_6) return miller_rabin<BASE_6, 6>(n_unsigned);
return miller_rabin<BASE_7, 7>(n_unsigned);
}
} // namespace suisen::miller_rabin
#line 1 "library/number/primitive_root.hpp"
#line 1 "library/number/order_Z_mZ.hpp"
#include <map>
#line 6 "library/number/order_Z_mZ.hpp"
#line 1 "library/number/fast_factorize.hpp"
#include <cmath>
#line 6 "library/number/fast_factorize.hpp"
#include <random>
#include <numeric>
#include <utility>
#line 1 "library/type_traits/type_traits.hpp"
#line 7 "library/type_traits/type_traits.hpp"
namespace suisen {
template <typename ...Constraints> using constraints_t = std::enable_if_t<std::conjunction_v<Constraints...>, std::nullptr_t>;
template <typename T, typename = std::nullptr_t> struct bitnum { static constexpr int value = 0; };
template <typename T> struct bitnum<T, constraints_t<std::is_integral<T>>> { static constexpr int value = std::numeric_limits<std::make_unsigned_t<T>>::digits; };
template <typename T> static constexpr int bitnum_v = bitnum<T>::value;
template <typename T, size_t n> struct is_nbit { static constexpr bool value = bitnum_v<T> == n; };
template <typename T, size_t n> static constexpr bool is_nbit_v = is_nbit<T, n>::value;
template <typename T, typename = std::nullptr_t> struct safely_multipliable { using type = T; };
template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 32>>> { using type = long long; };
template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 64>>> { using type = __int128_t; };
template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 32>>> { using type = unsigned long long; };
template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 64>>> { using type = __uint128_t; };
template <typename T> using safely_multipliable_t = typename safely_multipliable<T>::type;
template <typename T, typename = void> struct rec_value_type { using type = T; };
template <typename T> struct rec_value_type<T, std::void_t<typename T::value_type>> {
using type = typename rec_value_type<typename T::value_type>::type;
};
template <typename T> using rec_value_type_t = typename rec_value_type<T>::type;
template <typename T> class is_iterable {
template <typename T_> static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{});
static std::false_type test(...);
public:
static constexpr bool value = decltype(test(std::declval<T>()))::value;
};
template <typename T> static constexpr bool is_iterable_v = is_iterable<T>::value;
template <typename T> class is_writable {
template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::ostream&>() << e, std::true_type{});
static std::false_type test(...);
public:
static constexpr bool value = decltype(test(std::declval<T>()))::value;
};
template <typename T> static constexpr bool is_writable_v = is_writable<T>::value;
template <typename T> class is_readable {
template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::istream&>() >> e, std::true_type{});
static std::false_type test(...);
public:
static constexpr bool value = decltype(test(std::declval<T>()))::value;
};
template <typename T> static constexpr bool is_readable_v = is_readable<T>::value;
} // namespace suisen
#line 11 "library/number/fast_factorize.hpp"
#line 1 "library/number/sieve_of_eratosthenes.hpp"
#line 7 "library/number/sieve_of_eratosthenes.hpp"
#line 1 "library/number/internal_eratosthenes.hpp"
#line 6 "library/number/internal_eratosthenes.hpp"
namespace suisen::internal::sieve {
constexpr std::uint8_t K = 8;
constexpr std::uint8_t PROD = 2 * 3 * 5;
constexpr std::uint8_t RM[K] = { 1, 7, 11, 13, 17, 19, 23, 29 };
constexpr std::uint8_t DR[K] = { 6, 4, 2, 4, 2, 4, 6, 2 };
constexpr std::uint8_t DF[K][K] = {
{ 0, 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 1, 0, 1, 1, 1, 1 },
{ 2, 2, 0, 2, 0, 2, 2, 1 }, { 3, 1, 1, 2, 1, 1, 3, 1 },
{ 3, 3, 1, 2, 1, 3, 3, 1 }, { 4, 2, 2, 2, 2, 2, 4, 1 },
{ 5, 3, 1, 4, 1, 3, 5, 1 }, { 6, 4, 2, 4, 2, 4, 6, 1 },
};
constexpr std::uint8_t DRP[K] = { 48, 32, 16, 32, 16, 32, 48, 16 };
constexpr std::uint8_t DFP[K][K] = {
{ 0, 0, 0, 0, 0, 0, 0, 8 }, { 8, 8, 8, 0, 8, 8, 8, 8 },
{ 16, 16, 0, 16, 0, 16, 16, 8 }, { 24, 8, 8, 16, 8, 8, 24, 8 },
{ 24, 24, 8, 16, 8, 24, 24, 8 }, { 32, 16, 16, 16, 16, 16, 32, 8 },
{ 40, 24, 8, 32, 8, 24, 40, 8 }, { 48, 32, 16, 32, 16, 32, 48, 8 },
};
constexpr std::uint8_t MASK[K][K] = {
{ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, { 0x02, 0x20, 0x10, 0x01, 0x80, 0x08, 0x04, 0x40 },
{ 0x04, 0x10, 0x01, 0x40, 0x02, 0x80, 0x08, 0x20 }, { 0x08, 0x01, 0x40, 0x20, 0x04, 0x02, 0x80, 0x10 },
{ 0x10, 0x80, 0x02, 0x04, 0x20, 0x40, 0x01, 0x08 }, { 0x20, 0x08, 0x80, 0x02, 0x40, 0x01, 0x10, 0x04 },
{ 0x40, 0x04, 0x08, 0x80, 0x01, 0x10, 0x20, 0x02 }, { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 },
};
constexpr std::uint8_t OFFSET[K][K] = {
{ 0, 1, 2, 3, 4, 5, 6, 7, },
{ 1, 5, 4, 0, 7, 3, 2, 6, },
{ 2, 4, 0, 6, 1, 7, 3, 5, },
{ 3, 0, 6, 5, 2, 1, 7, 4, },
{ 4, 7, 1, 2, 5, 6, 0, 3, },
{ 5, 3, 7, 1, 6, 0, 4, 2, },
{ 6, 2, 3, 7, 0, 4, 5, 1, },
{ 7, 6, 5, 4, 3, 2, 1, 0, },
};
constexpr std::uint8_t mask_to_index(const std::uint8_t bits) {
switch (bits) {
case 1 << 0: return 0;
case 1 << 1: return 1;
case 1 << 2: return 2;
case 1 << 3: return 3;
case 1 << 4: return 4;
case 1 << 5: return 5;
case 1 << 6: return 6;
case 1 << 7: return 7;
default: assert(false);
}
}
} // namespace suisen::internal::sieve
#line 9 "library/number/sieve_of_eratosthenes.hpp"
namespace suisen {
template <unsigned int N>
class SimpleSieve {
private:
static constexpr unsigned int size = N / internal::sieve::PROD + 1;
static std::uint8_t flag[size];
public:
SimpleSieve() {
using namespace internal::sieve;
flag[0] |= 1;
unsigned int k_max = (unsigned int) std::sqrt(N + 2) / PROD;
for (unsigned int kp = 0; kp <= k_max; ++kp) {
for (std::uint8_t bits = ~flag[kp]; bits; bits &= bits - 1) {
const std::uint8_t mp = mask_to_index(bits & -bits), m = RM[mp];
unsigned int kr = kp * (PROD * kp + 2 * m) + m * m / PROD;
for (std::uint8_t mq = mp; kr < size; kr += kp * DR[mq] + DF[mp][mq], ++mq &= 7) {
flag[kr] |= MASK[mp][mq];
}
}
}
}
std::vector<int> prime_list(unsigned int max_val = N) const {
using namespace internal::sieve;
std::vector<int> res { 2, 3, 5 };
res.reserve(max_val / 25);
for (unsigned int i = 0, offset = 0; i < size and offset < max_val; ++i, offset += PROD) {
for (uint8_t f = ~flag[i]; f;) {
uint8_t g = f & -f;
res.push_back(offset + RM[mask_to_index(g)]);
f ^= g;
}
}
while (res.size() and (unsigned int) res.back() > max_val) res.pop_back();
return res;
}
bool is_prime(const unsigned int p) const {
using namespace internal::sieve;
switch (p) {
case 2: case 3: case 5: return true;
default:
switch (p % PROD) {
case RM[0]: return ((flag[p / PROD] >> 0) & 1) == 0;
case RM[1]: return ((flag[p / PROD] >> 1) & 1) == 0;
case RM[2]: return ((flag[p / PROD] >> 2) & 1) == 0;
case RM[3]: return ((flag[p / PROD] >> 3) & 1) == 0;
case RM[4]: return ((flag[p / PROD] >> 4) & 1) == 0;
case RM[5]: return ((flag[p / PROD] >> 5) & 1) == 0;
case RM[6]: return ((flag[p / PROD] >> 6) & 1) == 0;
case RM[7]: return ((flag[p / PROD] >> 7) & 1) == 0;
default: return false;
}
}
}
};
template <unsigned int N>
std::uint8_t SimpleSieve<N>::flag[SimpleSieve<N>::size];
template <unsigned int N>
class Sieve {
private:
static constexpr unsigned int base_max = (N + 1) * internal::sieve::K / internal::sieve::PROD;
static unsigned int pf[base_max + internal::sieve::K];
public:
Sieve() {
using namespace internal::sieve;
pf[0] = 1;
unsigned int k_max = ((unsigned int) std::sqrt(N + 1) - 1) / PROD;
for (unsigned int kp = 0; kp <= k_max; ++kp) {
const int base_i = kp * K, base_act_i = kp * PROD;
for (int mp = 0; mp < K; ++mp) {
const int m = RM[mp], i = base_i + mp;
if (pf[i] == 0) {
unsigned int act_i = base_act_i + m;
unsigned int base_k = (kp * (PROD * kp + 2 * m) + m * m / PROD) * K;
for (std::uint8_t mq = mp; base_k <= base_max; base_k += kp * DRP[mq] + DFP[mp][mq], ++mq &= 7) {
pf[base_k + OFFSET[mp][mq]] = act_i;
}
}
}
}
}
bool is_prime(const unsigned int p) const {
using namespace internal::sieve;
switch (p) {
case 2: case 3: case 5: return true;
default:
switch (p % PROD) {
case RM[0]: return pf[p / PROD * K + 0] == 0;
case RM[1]: return pf[p / PROD * K + 1] == 0;
case RM[2]: return pf[p / PROD * K + 2] == 0;
case RM[3]: return pf[p / PROD * K + 3] == 0;
case RM[4]: return pf[p / PROD * K + 4] == 0;
case RM[5]: return pf[p / PROD * K + 5] == 0;
case RM[6]: return pf[p / PROD * K + 6] == 0;
case RM[7]: return pf[p / PROD * K + 7] == 0;
default: return false;
}
}
}
int prime_factor(const unsigned int p) const {
using namespace internal::sieve;
switch (p % PROD) {
case 0: case 2: case 4: case 6: case 8:
case 10: case 12: case 14: case 16: case 18:
case 20: case 22: case 24: case 26: case 28: return 2;
case 3: case 9: case 15: case 21: case 27: return 3;
case 5: case 25: return 5;
case RM[0]: return pf[p / PROD * K + 0] ? pf[p / PROD * K + 0] : p;
case RM[1]: return pf[p / PROD * K + 1] ? pf[p / PROD * K + 1] : p;
case RM[2]: return pf[p / PROD * K + 2] ? pf[p / PROD * K + 2] : p;
case RM[3]: return pf[p / PROD * K + 3] ? pf[p / PROD * K + 3] : p;
case RM[4]: return pf[p / PROD * K + 4] ? pf[p / PROD * K + 4] : p;
case RM[5]: return pf[p / PROD * K + 5] ? pf[p / PROD * K + 5] : p;
case RM[6]: return pf[p / PROD * K + 6] ? pf[p / PROD * K + 6] : p;
case RM[7]: return pf[p / PROD * K + 7] ? pf[p / PROD * K + 7] : p;
default: assert(false);
}
}
/**
* Returns a vector of `{ prime, index }`.
*/
std::vector<std::pair<int, int>> factorize(unsigned int n) const {
assert(0 < n and n <= N);
std::vector<std::pair<int, int>> prime_powers;
while (n > 1) {
int p = prime_factor(n), c = 0;
do { n /= p, ++c; } while (n % p == 0);
prime_powers.emplace_back(p, c);
}
return prime_powers;
}
/**
* Returns the divisors of `n`.
* It is NOT guaranteed that the returned vector is sorted.
*/
std::vector<int> divisors(unsigned int n) const {
assert(0 < n and n <= N);
std::vector<int> divs { 1 };
for (auto [prime, index] : factorize(n)) {
int sz = divs.size();
for (int i = 0; i < sz; ++i) {
int d = divs[i];
for (int j = 0; j < index; ++j) {
divs.push_back(d *= prime);
}
}
}
return divs;
}
};
template <unsigned int N>
unsigned int Sieve<N>::pf[Sieve<N>::base_max + internal::sieve::K];
} // namespace suisen
#line 14 "library/number/fast_factorize.hpp"
namespace suisen::fast_factorize {
namespace internal {
template <typename T>
constexpr int floor_log2(T n) {
int i = 0;
while (n) n >>= 1, ++i;
return i - 1;
}
template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
T pollard_rho(const T n) {
using M = safely_multipliable_t<T>;
const T m = T(1) << (floor_log2(n) / 5);
static std::mt19937_64 rng{std::random_device{}()};
std::uniform_int_distribution<T> dist(0, n - 1);
// const Montgomery64 mg{n};
while (true) {
T c = dist(rng);
auto f = [&](T x) -> T { return (M(x) * x + c) % n; };
T x, y = 2, ys, q = 1, g = 1;
for (T r = 1; g == 1; r <<= 1) {
x = y;
for (T i = 0; i < r; ++i) y = f(y);
for (T k = 0; k < r and g == 1; k += m) {
ys = y;
for (T i = 0; i < std::min(m, r - k); ++i) y = f(y), q = M(q) * (x > y ? x - y : y - x) % n;
g = std::gcd(q, n);
}
}
if (g == n) {
g = 1;
while (g == 1) ys = f(ys), g = std::gcd(x > ys ? x - ys : ys - x, n);
}
if (g < n) {
if (miller_rabin::is_prime(g)) return g;
if (T d = n / g; miller_rabin::is_prime(d)) return d;
return pollard_rho(g);
}
}
}
}
template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
std::vector<std::pair<T, int>> factorize(T n) {
static constexpr int threshold = 1000000;
static Sieve<threshold> sieve;
std::vector<std::pair<T, int>> res;
if (n <= threshold) {
for (auto [p, q] : sieve.factorize(n)) res.emplace_back(p, q);
return res;
}
if ((n & 1) == 0) {
int q = 0;
do ++q, n >>= 1; while ((n & 1) == 0);
res.emplace_back(2, q);
}
for (T p = 3; p * p <= n; p += 2) {
if (p >= 101 and n >= 1 << 20) {
while (n > 1) {
if (miller_rabin::is_prime(n)) {
res.emplace_back(std::exchange(n, 1), 1);
} else {
p = internal::pollard_rho(n);
int q = 0;
do ++q, n /= p; while (n % p == 0);
res.emplace_back(p, q);
}
}
break;
}
if (n % p == 0) {
int q = 0;
do ++q, n /= p; while (n % p == 0);
res.emplace_back(p, q);
}
}
if (n > 1) res.emplace_back(n, 1);
return res;
}
} // namespace suisen::fast_factorize
#line 9 "library/number/order_Z_mZ.hpp"
namespace suisen {
namespace internal::order_prime_mod {
template <int id>
struct mint64 {
static uint64_t mod() { return _mod; }
static void set_mod(uint64_t new_mod) { mint64<id>::_mod = new_mod; }
mint64() : _val(0) {}
mint64(long long val) : _val(safe_mod(val)) {}
uint64_t val() { return _val; }
friend mint64& operator*=(mint64& x, const mint64& y) {
x._val = __uint128_t(x._val) * y._val % _mod;
return x;
}
friend mint64 operator*(mint64 x, const mint64& y) {
x *= y;
return x;
}
mint64 pow(long long b) const {
assert(b >= 0);
mint64 p = *this, res = 1;
for (; b; b >>= 1) {
if (b & 1) res *= p;
p *= p;
}
return res;
}
private:
static inline uint64_t _mod;
uint64_t _val;
static uint64_t safe_mod(long long val) { return (val %= _mod) < 0 ? val + _mod : val; }
};
}
template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
struct OrderMod {
using U = std::make_unsigned_t<T>;
OrderMod() = default;
OrderMod(T m) : _mod(m) {
auto factorized = fast_factorize::factorize<T>(_mod);
_is_prime = factorized.size() == 1;
_lambda = _carmichael(factorized);
_phi = _totient(factorized);
for (auto [p, q] : fast_factorize::factorize<T>(_lambda)) {
U r = 1;
for (int i = 0; i < q; ++i) r *= p;
_fac_lambda.emplace_back(p, q, r);
}
}
bool is_primitive_root(U a) const {
if (_mod < 1ULL << 32) {
using mint = atcoder::dynamic_modint<1000000000>;
U old_mod = mint::mod();
mint::set_mod(_mod);
bool res = _is_primitive_root_impl<mint>(a);
mint::set_mod(old_mod);
return res;
} else {
using mint = internal::order_prime_mod::mint64<1000000000>;
U old_mod = mint::mod();
mint::set_mod(_mod);
bool res = _is_primitive_root_impl<mint>(a);
mint::set_mod(old_mod);
return res;
}
}
T primitive_root() const {
assert(_lambda == _phi);
if (_mod < 1ULL << 32) {
return _primitive_root_impl<std::mt19937>();
} else {
return _primitive_root_impl<std::mt19937_64>();
}
}
T operator()(U a) const {
if (_mod < 1ULL << 31) {
using mint = atcoder::dynamic_modint<1000000000>;
U old_mod = mint::mod();
mint::set_mod(_mod);
T res = _order_impl<mint>(a);
mint::set_mod(old_mod);
return res;
} else {
using mint = internal::order_prime_mod::mint64<1000000000>;
U old_mod = mint::mod();
mint::set_mod(_mod);
T res = _order_impl<mint>(a);
mint::set_mod(old_mod);
return res;
}
}
T mod() const {
return _mod;
}
T totient() const {
return _phi;
}
T carmichael() const {
return _lambda;
}
bool is_prime() const {
return _is_prime;
}
std::vector<T> carmichael_prime_factors() const {
std::vector<T> res;
for (const auto &e : _fac_lambda) res.push_back(std::get<0>(e));
return res;
}
private:
U _mod;
U _phi;
U _lambda;
bool _is_prime;
std::vector<std::tuple<U, int, U>> _fac_lambda;
static T _carmichael(const std::vector<std::pair<T, int>>& factorized) {
T lambda = 1;
for (auto [p, ep] : factorized) {
T phi = p - 1;
int exponent = ep - (1 + (p == 2 and ep >= 3));
for (int i = 0; i < exponent; ++i) phi *= p;
lambda = std::lcm(lambda, phi);
}
return lambda;
}
static T _totient(const std::vector<std::pair<T, int>>& factorized) {
T t = 1;
for (const auto& [p, ep] : factorized) {
t *= p - 1;
for (int i = 0; i < ep - 1; ++i) t *= p;
}
return t;
}
template <typename mint>
bool _is_primitive_root_impl(U a) const {
if (_lambda != _phi) return false;
if (_mod == 2) return a % 2 == 1;
const int k = _fac_lambda.size();
U x = _lambda;
for (const auto& [p, q, pq] : _fac_lambda) x /= p;
mint b = mint(a).pow(x);
if (k == 1) return b.val() != 1;
auto dfs = [&](auto dfs, const int l, const int r, const mint val) -> bool {
const int m = (l + r) >> 1;
U lp = 1;
for (int i = m; i < r; ++i) lp *= std::get<0>(_fac_lambda[i]);
mint lval = val.pow(lp);
if (m - l == 1) {
if (lval.val() == 1) return false;
} else {
if (not dfs(dfs, l, m, lval)) return false;
}
U rp = 1;
for (int i = l; i < m; ++i) rp *= std::get<0>(_fac_lambda[i]);
mint rval = val.pow(rp);
if (r - m == 1) {
if (rval.val() == 1) return false;
} else {
if (not dfs(dfs, m, r, rval)) return false;
}
return true;
};
return dfs(dfs, 0, k, b);
}
template <typename Rng>
T _primitive_root_impl() const {
if (_mod == 2) return 1;
Rng rng{ std::random_device{}() };
while (true) {
U a = rng() % (_mod - 2) + 2;
while (not _is_prime and std::gcd(a, _mod) != 1) {
a = rng() % (_mod - 2) + 2;
}
if (is_primitive_root(a)) return a;
}
}
template <typename mint>
U _order_impl(U a) const {
if (_mod == 2) return a % 2 == 1;
const int k = _fac_lambda.size();
U res = 1;
auto update = [&](U p, mint val) {
while (val.val() != 1) {
val = val.pow(p);
res *= p;
}
};
if (k == 1) {
update(std::get<0>(_fac_lambda.front()), a);
return res;
}
auto dfs = [&](auto dfs, const int l, const int r, const mint val) -> void {
const int m = (l + r) >> 1;
U lp = 1;
for (int i = m; i < r; ++i) lp *= std::get<2>(_fac_lambda[i]);
mint lval = val.pow(lp);
if (m - l == 1) {
update(std::get<0>(_fac_lambda[l]), lval);
} else {
dfs(dfs, l, m, lval);
}
U rp = 1;
for (int i = l; i < m; ++i) rp *= std::get<2>(_fac_lambda[i]);
mint rval = val.pow(rp);
if (r - m == 1) {
update(std::get<0>(_fac_lambda[m]), rval);
} else {
dfs(dfs, m, r, rval);
}
};
dfs(dfs, 0, k, a);
return res;
}
};
} // namespace suisen
#line 5 "library/number/primitive_root.hpp"
namespace suisen {
template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
T primitive_root(T p) {
return OrderMod<T>{p}.primitive_root();
}
} // namespace suisen
#line 1 "library/number/garner.hpp"
#line 1 "library/number/ext_gcd.hpp"
#line 7 "library/number/ext_gcd.hpp"
#include <optional>
#line 10 "library/number/ext_gcd.hpp"
namespace suisen {
constexpr long long safe_mod(long long x, long long m) {
x %= m;
return x < 0 ? x + m : x;
}
// returns {x,y,g} s.t. ax+by=g=gcd(a,b)>=0.
std::tuple<long long, long long, long long> ext_gcd(long long a, long long b) {
long long x = 1, y = 0;
long long z = 0, w = 1;
while (b) {
long long p = a / b, q = a % b;
x -= y * p, std::swap(x, y);
z -= w * p, std::swap(z, w);
a = b, b = q;
}
if (a < 0) {
x = -x, z = -z, a = -a;
}
return { x, z, a };
}
// returns {x,g} s.t. a*x=g (mod m)
std::pair<long long, long long> gcd_inv(long long a, long long m) {
auto [x, y, g] = ext_gcd(a, m);
return { safe_mod(x, m), g };
}
// returns x s.t. a*x=1 (mod m) if exists, otherwise throws runtime error.
long long inv_mod(long long a, long long mod) {
auto [inv, y, g] = ext_gcd(a, mod);
assert(g == 1);
return safe_mod(inv, mod);
}
} // namespace suisen
#line 6 "library/number/garner.hpp"
namespace suisen {
/**
* @brief Calculates x mod m s.t. x = x_i (mod m_i). m_i should be coprime each other.
* @param eq vector of { x_i, m_i }
* @return x mod m s.t. x = x_i (mod m_i)
*/
int garner(std::vector<std::pair<int, int>> eq, int m) {
const int n = eq.size();
std::vector<long long> a(n);
auto calc_prefix = [&](int i, long long mod) {
long long res = 0;
long long prd = 1;
for (int j = 0; j < i; ++j) {
(res += a[j] * prd) %= mod;
(prd *= eq[j].second) %= mod;
}
return res;
};
for (int i = 0; i < n; ++i) {
auto [xi, mi] = eq[i];
a[i] = (xi - calc_prefix(i, mi)) % mi;
if (a[i] < 0) a[i] += mi;
for (int j = 0; j < i; ++j) {
long long mj = eq[j].second;
a[i] *= inv_mod(mj, mi);
a[i] %= mi;
}
}
return calc_prefix(n, m);
}
} // namespace suisen
#line 12 "library/convolution/multi_variate_convolution_circular.hpp"
namespace suisen {
namespace internal {
template <typename mint, std::enable_if_t<atcoder::internal::is_modint<mint>::value, std::nullptr_t> = nullptr>
struct multi_variate_convolution_circular {
multi_variate_convolution_circular() = default;
multi_variate_convolution_circular(std::vector<int> n) : _d(n.size()), _l(std::reduce(n.begin(), n.end(), 1, std::multiplies<int>())), _n(n), _g(_d), _ig(_d) {
assert(miller_rabin::is_prime(mint::mod()));
mint g = primitive_root(mint::mod());
for (int i = 0; i < _d; ++i) {
assert((mint::mod() - 1) % n[i] == 0);
_g[i] = g.pow((mint::mod() - 1) / n[i]);
_ig[i] = _g[i].inv();
}
}
std::vector<mint> convolution(std::vector<mint> f, std::vector<mint> g) {
fft(f, false), fft(g, false);
for (int i = 0; i < _l; ++i) f[i] *= g[i];
fft(f, true);
return f;
}
std::vector<mint> operator()(const std::vector<mint>& f, const std::vector<mint>& g) {
return convolution(f, g);
}
private:
int _d, _l;
std::vector<int> _n;
std::vector<mint> _g, _ig;
void fft(std::vector<mint>& f, bool inverse) {
const auto& g = inverse ? _g : _ig;
for (int i = 0, block = 1; i < _d; ++i) {
std::vector<mint> x(_n[i]);
const int nblock = block * _n[i];
for (int l = 0; l + nblock <= _l; l += nblock) {
for (int start = l; start < l + block; ++start) {
if (_n[i] == 2) {
mint u = f[start], v = f[start + block];
f[start] = u + v;
f[start + block] = u - v;
} else if (_n[i] == 3) {
mint u = f[start], v = f[start + block], w = f[start + block + block];
f[start] = u + v + w;
f[start + block] = u + (v + w * g[i]) * g[i];
f[start + block + block] = u + (w + v * g[i]) * g[i];
} else {
for (int p = 0; p < _n[i]; ++p) x[p] = f[start + p * block];
if (_n[i] <= 100) {
x = internal::chirp_z_transform_naive<mint>(x, 1, g[i], _n[i]);
} else {
x = chirp_z_transform<mint>(x, 1, g[i], _n[i], arbitrary_mod_convolution<mint>);
}
for (int p = 0; p < _n[i]; ++p) f[start + p * block] = x[p];
}
}
}
block = nblock;
}
if (inverse) {
mint inv_size = mint(f.size()).inv();
for (auto& e : f) e *= inv_size;
}
}
};
}
template <typename mint, std::enable_if_t<atcoder::internal::is_modint<mint>::value, std::nullptr_t> = nullptr>
struct multi_variate_convolution_circular {
private:
using mint2 = atcoder::dynamic_modint<102938478>;
public:
multi_variate_convolution_circular() = default;
multi_variate_convolution_circular(std::vector<int> n) : _d(n.size()), _l(std::reduce(n.begin(), n.end(), 1, std::multiplies<int>())), _n(n) {
const __int128_t max_val = __int128_t(mint::mod() - 1) * (mint::mod() - 1) * _l;
const int t = std::reduce(n.begin(), n.end(), 1, [](int x, int y) { return std::lcm(x, y); });
if ((mint::mod() - 1) % t == 0) {
_mods = { mint::mod() };
} else {
__int128_t prod = 1;
for (int k = 1000000000 / t; k >= 0; --k) if (const int p = k * t + 1; miller_rabin::is_prime(p)) {
_mods.push_back(p);
prod *= p;
if (prod >= max_val) break;
}
assert(prod >= max_val);
}
const int m = _mods.size();
_cnvs.resize(m);
for (int i = 0; i < m; ++i) with_kth_mod(i, [&, this] {
_cnvs[i] = internal::multi_variate_convolution_circular<mint2>(_n);
});
}
std::vector<mint> convolution(std::vector<mint> f, const std::vector<mint>& g) {
assert(int(f.size()) == _l and int(g.size()) == _l);
if (_d == 0) return { f[0] * g[0] };
// if (_d == 1) return arbitrary_mod_convolution<mint>(f, g);
if (_l <= 60) return convolution_naive(f, g);
const int m = _mods.size();
std::vector res(m, std::vector<int>(_l));
for (int i = 0; i < m; ++i) with_kth_mod(i, [&, this] {
std::vector<mint2> f2(_l), g2(_l);
for (int j = 0; j < _l; ++j) f2[j] = f[j].val(), g2[j] = g[j].val();
std::vector<mint2> h = _cnvs[i](f2, g2);
for (int j = 0; j < _l; ++j) res[i][j] = h[j].val();
});
std::vector<mint> h(_l);
for (int j = 0; j < _l; ++j) {
std::vector<std::pair<int, int>> eq(m);
for (int i = 0; i < m; ++i) {
eq[i] = { res[i][j], _mods[i] };
}
h[j] = garner(eq, mint::mod());
}
return h;
}
std::vector<mint> operator()(const std::vector<mint>& f, const std::vector<mint>& g) {
return convolution(f, g);
}
std::vector<mint> convolution_naive(const std::vector<mint>& f, const std::vector<mint>& g) {
std::vector<mint> h(_l);
for (int i = 0; i < _l; ++i) for (int j = 0; j < _l; ++j) {
int k = 0;
for (int d = 0, i2 = i, j2 = j, prd = 1; d < _d; ++d) {
k += prd * ((i2 + j2) % _n[d]);
i2 /= _n[d], j2 /= _n[d], prd *= _n[d];
}
h[k] += f[i] * g[j];
}
return h;
}
private:
int _d, _l;
std::vector<int> _n;
std::vector<int> _mods;
std::vector<internal::multi_variate_convolution_circular<mint2>> _cnvs;
template <typename F>
void with_kth_mod(int k, F&& f) {
int old_mod = mint2::mod();
mint2::set_mod(_mods[k]);
f();
if (old_mod >= 1) mint2::set_mod(old_mod);
}
};
} // namespace suisen
#line 10 "test/src/convolution/multi_variate_convolution_circular/dummy.test.cpp"
void test1() {
using namespace suisen;
std::vector<int> n { 2, 45, 73 };
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 6570
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 123109233);
std::iota(g.begin(), g.end(), 213082409);
std::vector<mint> h_expected = conv.convolution_naive(f, g);
std::vector<mint> h_actual = conv.convolution(f, g);
assert(h_expected == h_actual);
}
void test2() {
using namespace suisen;
std::vector<int> n { 2, 3, 2, 4, 3, 5 };
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 720
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 12038);
std::iota(g.begin(), g.end(), 4392);
std::vector<mint> h_expected = conv.convolution_naive(f, g);
std::vector<mint> h_actual = conv.convolution(f, g);
assert(h_expected == h_actual);
}
void test3() {
using namespace suisen;
std::vector<int> n {};
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 1
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 4);
std::iota(g.begin(), g.end(), 3);
std::vector<mint> h_expected = conv.convolution_naive(f, g);
std::vector<mint> h_actual = conv.convolution(f, g);
assert(h_expected == h_actual);
}
#line 1 "library/util/timer.hpp"
#include <chrono>
namespace suisen {
struct Timer {
using minutes_t = std::chrono::minutes;
using seconds_t = std::chrono::seconds;
using milliseconds_t = std::chrono::milliseconds;
using microseconds_t = std::chrono::microseconds;
using nanoseconds_t = std::chrono::nanoseconds;
Timer() { start(); }
void start() {
_start = std::chrono::system_clock::now();
}
template <typename TimeUnit = std::chrono::milliseconds>
double elapsed() const {
return std::chrono::duration_cast<TimeUnit>(std::chrono::system_clock::now() - _start).count();
}
template <typename TimeUnit = std::chrono::milliseconds, typename Proc>
static double measure(Proc &&proc) {
Timer timer;
proc();
return timer.elapsed<TimeUnit>();
}
private:
decltype(std::chrono::system_clock::now()) _start;
};
} // namespace suisen
#line 66 "test/src/convolution/multi_variate_convolution_circular/dummy.test.cpp"
void perf_test1() {
using namespace suisen;
std::vector<int> n(18, 2);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test2() {
using namespace suisen;
std::vector<int> n(11, 3);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 177147
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test3() {
using namespace suisen;
std::vector<int> n { 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 236196
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test4() {
using namespace suisen;
std::vector<int> n(9, 4);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test5() {
using namespace suisen;
std::vector<int> n(7, 6);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 279936
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test6() {
using namespace suisen;
std::vector<int> n(6, 8);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test7() {
using namespace suisen;
std::vector<int> n(5, 12);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 248832
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test8() {
using namespace suisen;
std::vector<int> n(4, 22);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 234256
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test9() {
using namespace suisen;
std::vector<int> n(3, 64);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test10() {
using namespace suisen;
std::vector<int> n(2, 512);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void perf_test11() {
using namespace suisen;
std::vector<int> n(1, 262144);
const int l = std::reduce(n.begin(), n.end(), 1, std::multiplies<int>()); // 262144
suisen::multi_variate_convolution_circular<mint> conv(n);
std::vector<mint> f(l), g(l);
std::iota(f.begin(), f.end(), 2348042);
std::iota(g.begin(), g.end(), 5439850);
Timer t;
std::vector<mint> h_actual = conv.convolution(f, g);
double elapsed = t.elapsed();
std::cerr << "Solved in " << elapsed << " ms." << std::endl;
if (elapsed > 2000) {
std::cerr << "TLE" << std::endl;
assert(false);
}
}
void test() {
test1();
test2();
test3();
perf_test1();
perf_test2();
perf_test3();
perf_test4();
perf_test5();
perf_test6();
perf_test7();
perf_test8();
perf_test9();
perf_test10();
perf_test11();
}
int main() {
test();
std::cout << "Hello World" << std::endl;
return 0;
}