This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/shift_of_sampling_points_of_polynomial
#include <iostream>
#include <ranges>
#include <vector>
#include "atcoder/modint.hpp"
#include "tools/sample_point_shift.hpp"
#include "tools/join.hpp"
using mint = atcoder::modint998244353;
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int N, M;
std::cin >> N >> M;
mint c;
{
int c_int;
std::cin >> c_int;
c = mint::raw(c_int);
}
std::vector<mint> f(N);
for (int i = 0; i < N; ++i) {
int f_i;
std::cin >> f_i;
f[i] = mint::raw(f_i);
}
const auto g = tools::sample_point_shift(f.begin(), f.end(), c, M);
std::cout << tools::join(g | std::views::transform([](const auto g_i) { return g_i.val(); }), " ") << '\n';
return 0;
}
#line 1 "tests/sample_point_shift.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/shift_of_sampling_points_of_polynomial
#include <iostream>
#include <ranges>
#include <vector>
#line 1 "lib/ac-library/atcoder/modint.hpp"
#include <cassert>
#include <numeric>
#include <type_traits>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#line 1 "lib/ac-library/atcoder/internal_math.hpp"
#include <utility>
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long 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
struct barrett {
unsigned int _m;
unsigned long long im;
// @param m `1 <= m`
explicit barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}
// @return m
unsigned int umod() const { return _m; }
// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
unsigned int mul(unsigned int a, unsigned int b) const {
// [1] m = 1
// a = b = im = 0, so okay
// [2] m >= 2
// im = ceil(2^64 / m)
// -> im * m = 2^64 + r (0 <= r < m)
// let z = a*b = c*m + d (0 <= c, d < m)
// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
// c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
// ((ab * im) >> 64) == c or c + 1
unsigned long long z = a;
z *= b;
#ifdef _MSC_VER
unsigned long long x;
_umul128(z, im, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
unsigned long long y = x * _m;
return (unsigned int)(z - y + (z < y ? _m : 0));
}
};
// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
if (m == 1) return 0;
unsigned int _m = (unsigned int)(m);
unsigned long long r = 1;
unsigned long long y = safe_mod(x, m);
while (n) {
if (n & 1) r = (r * y) % _m;
y = (y * y) % _m;
n >>= 1;
}
return r;
}
// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
if (n <= 1) return false;
if (n == 2 || n == 7 || n == 61) return true;
if (n % 2 == 0) return false;
long long d = n - 1;
while (d % 2 == 0) d /= 2;
constexpr long long bases[3] = {2, 7, 61};
for (long long a : bases) {
long long t = d;
long long y = pow_mod_constexpr(a, t, n);
while (t != n - 1 && y != 1 && y != n - 1) {
y = y * y % n;
t <<= 1;
}
if (y != n - 1 && t % 2 == 0) {
return false;
}
}
return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);
// @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<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
long long s = b, t = a;
long long m0 = 0, m1 = 1;
while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
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; (long long)(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);
// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
unsigned long long m,
unsigned long long a,
unsigned long long b) {
unsigned long long ans = 0;
while (true) {
if (a >= m) {
ans += n * (n - 1) / 2 * (a / m);
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}
unsigned long long y_max = a * n + b;
if (y_max < m) break;
// y_max < m * (n + 1)
// floor(y_max / m) <= n
n = (unsigned long long)(y_max / m);
b = (unsigned long long)(y_max % m);
std::swap(m, a);
}
return ans;
}
} // namespace internal
} // namespace atcoder
#line 1 "lib/ac-library/atcoder/internal_type_traits.hpp"
#line 7 "lib/ac-library/atcoder/internal_type_traits.hpp"
namespace atcoder {
namespace internal {
#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value ||
std::is_same<T, __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int128 =
typename std::conditional<std::is_same<T, __uint128_t>::value ||
std::is_same<T, unsigned __int128>::value,
std::true_type,
std::false_type>::type;
template <class T>
using make_unsigned_int128 =
typename std::conditional<std::is_same<T, __int128_t>::value,
__uint128_t,
unsigned __int128>;
template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
is_signed_int128<T>::value ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
std::is_signed<T>::value) ||
is_signed_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<(is_integral<T>::value &&
std::is_unsigned<T>::value) ||
is_unsigned_int128<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<
is_signed_int128<T>::value,
make_unsigned_int128<T>,
typename std::conditional<std::is_signed<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type>::type;
#else
template <class T> using is_integral = typename std::is_integral<T>;
template <class T>
using is_signed_int =
typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using is_unsigned_int =
typename std::conditional<is_integral<T>::value &&
std::is_unsigned<T>::value,
std::true_type,
std::false_type>::type;
template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
std::make_unsigned<T>,
std::common_type<T>>::type;
#endif
template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;
template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;
template <class T> using to_unsigned_t = typename to_unsigned<T>::type;
} // namespace internal
} // namespace atcoder
#line 14 "lib/ac-library/atcoder/modint.hpp"
namespace atcoder {
namespace internal {
struct modint_base {};
struct static_modint_base : modint_base {};
template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;
} // namespace internal
template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
using mint = static_modint;
public:
static constexpr int mod() { return m; }
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
static_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
static_modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
static_modint(T v) {
_v = (unsigned int)(v % umod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
mint& operator*=(const mint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
if (prime) {
assert(_v);
return pow(umod() - 2);
} else {
auto eg = internal::inv_gcd(_v, m);
assert(eg.first == 1);
return eg.second;
}
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static constexpr unsigned int umod() { return m; }
static constexpr bool prime = internal::is_prime<m>;
};
template <int id> struct dynamic_modint : internal::modint_base {
using mint = dynamic_modint;
public:
static int mod() { return (int)(bt.umod()); }
static void set_mod(int m) {
assert(1 <= m);
bt = internal::barrett(m);
}
static mint raw(int v) {
mint x;
x._v = v;
return x;
}
dynamic_modint() : _v(0) {}
template <class T, internal::is_signed_int_t<T>* = nullptr>
dynamic_modint(T v) {
long long x = (long long)(v % (long long)(mod()));
if (x < 0) x += mod();
_v = (unsigned int)(x);
}
template <class T, internal::is_unsigned_int_t<T>* = nullptr>
dynamic_modint(T v) {
_v = (unsigned int)(v % mod());
}
unsigned int val() const { return _v; }
mint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
mint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint& operator+=(const mint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator-=(const mint& rhs) {
_v += mod() - rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
mint& operator*=(const mint& rhs) {
_v = bt.mul(_v, rhs._v);
return *this;
}
mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
mint pow(long long n) const {
assert(0 <= n);
mint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
mint inv() const {
auto eg = internal::inv_gcd(_v, mod());
assert(eg.first == 1);
return eg.second;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs._v != rhs._v;
}
private:
unsigned int _v;
static internal::barrett bt;
static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt(998244353);
using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;
namespace internal {
template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;
template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;
template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};
template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;
} // namespace internal
} // namespace atcoder
#line 1 "tools/sample_point_shift.hpp"
#line 5 "tools/sample_point_shift.hpp"
#include <iterator>
#line 7 "tools/sample_point_shift.hpp"
#include <array>
#line 9 "tools/sample_point_shift.hpp"
#include <initializer_list>
#include <algorithm>
#line 1 "tools/is_prime.hpp"
#line 1 "tools/prod_mod.hpp"
#line 1 "tools/uint128_t.hpp"
#line 1 "tools/detail/int128_t.hpp"
#line 6 "tools/detail/int128_t.hpp"
#include <cstddef>
#include <cstdint>
#include <functional>
#line 10 "tools/detail/int128_t.hpp"
#include <limits>
#include <string>
#include <string_view>
#line 1 "tools/abs.hpp"
namespace tools {
constexpr float abs(const float x) {
return x < 0 ? -x : x;
}
constexpr double abs(const double x) {
return x < 0 ? -x : x;
}
constexpr long double abs(const long double x) {
return x < 0 ? -x : x;
}
constexpr int abs(const int x) {
return x < 0 ? -x : x;
}
constexpr long abs(const long x) {
return x < 0 ? -x : x;
}
constexpr long long abs(const long long x) {
return x < 0 ? -x : x;
}
constexpr unsigned int abs(const unsigned int x) {
return x;
}
constexpr unsigned long abs(const unsigned long x) {
return x;
}
constexpr unsigned long long abs(const unsigned long long x) {
return x;
}
}
#line 1 "tools/bit_ceil.hpp"
#include <bit>
#line 1 "tools/is_integral.hpp"
#line 5 "tools/is_integral.hpp"
namespace tools {
template <typename T>
struct is_integral : ::std::is_integral<T> {};
template <typename T>
inline constexpr bool is_integral_v = ::tools::is_integral<T>::value;
}
#line 1 "tools/is_signed.hpp"
#line 5 "tools/is_signed.hpp"
namespace tools {
template <typename T>
struct is_signed : ::std::is_signed<T> {};
template <typename T>
inline constexpr bool is_signed_v = ::tools::is_signed<T>::value;
}
#line 1 "tools/make_unsigned.hpp"
#line 5 "tools/make_unsigned.hpp"
namespace tools {
template <typename T>
struct make_unsigned : ::std::make_unsigned<T> {};
template <typename T>
using make_unsigned_t = typename ::tools::make_unsigned<T>::type;
}
#line 10 "tools/bit_ceil.hpp"
namespace tools {
template <typename T>
constexpr T bit_ceil(T) noexcept;
template <typename T>
constexpr T bit_ceil(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_ceil<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_ceil(x);
}
}
}
#line 1 "tools/bit_floor.hpp"
#line 10 "tools/bit_floor.hpp"
namespace tools {
template <typename T>
constexpr T bit_floor(T) noexcept;
template <typename T>
constexpr T bit_floor(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_floor<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_floor(x);
}
}
}
#line 1 "tools/bit_width.hpp"
#line 10 "tools/bit_width.hpp"
namespace tools {
template <typename T>
constexpr int bit_width(T) noexcept;
template <typename T>
constexpr int bit_width(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::tools::bit_width<::tools::make_unsigned_t<T>>(x);
} else {
return ::std::bit_width(x);
}
}
}
#line 1 "tools/countr_zero.hpp"
#line 12 "tools/countr_zero.hpp"
namespace tools {
template <typename T>
constexpr int countr_zero(const T x) noexcept {
static_assert(::tools::is_integral_v<T> && !::std::is_same_v<::std::remove_cv_t<T>, bool>);
if constexpr (::tools::is_signed_v<T>) {
assert(x >= 0);
return ::std::min(::tools::countr_zero<::tools::make_unsigned_t<T>>(x), ::std::numeric_limits<T>::digits);
} else {
return ::std::countr_zero(x);
}
}
}
#line 1 "tools/is_unsigned.hpp"
#line 5 "tools/is_unsigned.hpp"
namespace tools {
template <typename T>
struct is_unsigned : ::std::is_unsigned<T> {};
template <typename T>
inline constexpr bool is_unsigned_v = ::tools::is_unsigned<T>::value;
}
#line 1 "tools/hash_combine.hpp"
#line 6 "tools/hash_combine.hpp"
// Source: https://github.com/google/cityhash/blob/f5dc54147fcce12cefd16548c8e760d68ac04226/src/city.h
// License: MIT
// Author: Google Inc.
// Copyright (c) 2011 Google, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace tools {
template <typename T>
void hash_combine(::std::size_t& seed, const T& v) {
static const ::std::hash<T> hasher;
static constexpr ::std::size_t k_mul = 0x9ddfea08eb382d69ULL;
::std::size_t a = (hasher(v) ^ seed) * k_mul;
a ^= (a >> 47);
::std::size_t b = (seed ^ a) * k_mul;
b ^= (b >> 47);
seed = b * k_mul;
}
}
#line 1 "tools/make_signed.hpp"
#line 5 "tools/make_signed.hpp"
namespace tools {
template <typename T>
struct make_signed : ::std::make_signed<T> {};
template <typename T>
using make_signed_t = typename ::tools::make_signed<T>::type;
}
#line 1 "tools/now.hpp"
#include <chrono>
namespace tools {
inline long long now() {
return ::std::chrono::duration_cast<::std::chrono::nanoseconds>(::std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
}
#line 25 "tools/detail/int128_t.hpp"
namespace tools {
using uint128_t = unsigned __int128;
using int128_t = __int128;
namespace detail {
namespace int128_t {
constexpr ::tools::uint128_t parse_unsigned(const ::std::string_view s) noexcept {
assert(!s.empty());
::tools::uint128_t x = 0;
::std::size_t i = s[0] == '+';
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x <<= 4;
if ('0' <= s[i] && s[i] <= '9') {
x |= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x |= s[i] - 'a' + 10;
} else {
x |= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x += s[i] - '0';
}
}
return x;
}
constexpr ::tools::int128_t parse_signed(const ::std::string_view s) noexcept {
assert(!s.empty());
::tools::int128_t x = 0;
if (s[0] == '-') {
::std::size_t i = 1;
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x *= 16;
if ('0' <= s[i] && s[i] <= '9') {
x -= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x -= s[i] - 'a' + 10;
} else {
x -= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x -= s[i] - '0';
}
}
} else {
::std::size_t i = s[0] == '+';
if (i + 1 < s.size() && s[i] == '0' && (s[i + 1] == 'x' || s[i + 1] == 'X')) {
for (i += 2; i < s.size(); ++i) {
assert(('0' <= s[i] && s[i] <= '9') || ('a' <= s[i] && s[i] <= 'f') || ('A' <= s[i] && s[i] <= 'F'));
x <<= 4;
if ('0' <= s[i] && s[i] <= '9') {
x |= s[i] - '0';
} else if ('a' <= s[i] && s[i] <= 'f') {
x |= s[i] - 'a' + 10;
} else {
x |= s[i] - 'A' + 10;
}
}
} else {
for (; i < s.size(); ++i) {
assert('0' <= s[i] && s[i] <= '9');
x *= 10;
x += s[i] - '0';
}
}
}
return x;
}
}
}
constexpr ::tools::uint128_t abs(const ::tools::uint128_t& x) noexcept {
return x;
}
constexpr ::tools::int128_t abs(const ::tools::int128_t& x) {
return x >= 0 ? x : -x;
}
}
#define UINT128_C(c) ::tools::detail::int128_t::parse_unsigned(#c)
#define INT128_C(c) ::tools::detail::int128_t::parse_signed(#c)
inline ::std::istream& operator>>(::std::istream& is, ::tools::uint128_t& x) {
::std::string s;
is >> s;
x = ::tools::detail::int128_t::parse_unsigned(s);
return is;
}
inline ::std::istream& operator>>(::std::istream& is, ::tools::int128_t& x) {
::std::string s;
is >> s;
x = ::tools::detail::int128_t::parse_signed(s);
return is;
}
inline ::std::ostream& operator<<(::std::ostream& os, ::tools::uint128_t x) {
::std::string s;
if (x > 0) {
while (x > 0) {
s.push_back('0' + x % 10);
x /= 10;
}
} else {
s.push_back('0');
}
::std::ranges::reverse(s);
return os << s;
}
inline ::std::ostream& operator<<(::std::ostream& os, ::tools::int128_t x) {
::std::string s;
if (x > 0) {
while (x > 0) {
s.push_back('0' + x % 10);
x /= 10;
}
} else if (x < 0) {
while (x < 0) {
s.push_back('0' + (-(x % 10)));
x /= 10;
}
s.push_back('-');
} else {
s.push_back('0');
}
::std::ranges::reverse(s);
return os << s;
}
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
namespace std {
template <>
struct hash<::tools::uint128_t> {
::std::size_t operator()(const ::tools::uint128_t& x) const {
static const ::std::size_t seed = ::tools::now();
::std::size_t hash = seed;
::tools::hash_combine(hash, static_cast<::std::uint64_t>(x >> 64));
::tools::hash_combine(hash, static_cast<::std::uint64_t>(x & ((UINT128_C(1) << 64) - 1)));
return hash;
}
};
template <>
struct hash<::tools::int128_t> {
::std::size_t operator()(const ::tools::int128_t& x) const {
static ::std::hash<::tools::uint128_t> hasher;
return hasher(static_cast<::tools::uint128_t>(x));
}
};
}
#endif
namespace tools {
template <>
struct is_integral<::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<const ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<const ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_integral<const volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_integral<const volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_signed<::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<const ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<const ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<volatile ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_signed<const volatile ::tools::int128_t> : ::std::true_type {};
template <>
struct is_signed<const volatile ::tools::uint128_t> : ::std::false_type {};
template <>
struct is_unsigned<::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<const ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<const ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<volatile ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct is_unsigned<const volatile ::tools::int128_t> : ::std::false_type {};
template <>
struct is_unsigned<const volatile ::tools::uint128_t> : ::std::true_type {};
template <>
struct make_signed<::tools::int128_t> {
using type = ::tools::int128_t;
};
template <>
struct make_signed<::tools::uint128_t> {
using type = ::tools::int128_t;
};
template <>
struct make_signed<const ::tools::int128_t> {
using type = const ::tools::int128_t;
};
template <>
struct make_signed<const ::tools::uint128_t> {
using type = const ::tools::int128_t;
};
template <>
struct make_signed<volatile ::tools::int128_t> {
using type = volatile ::tools::int128_t;
};
template <>
struct make_signed<volatile ::tools::uint128_t> {
using type = volatile ::tools::int128_t;
};
template <>
struct make_signed<const volatile ::tools::int128_t> {
using type = const volatile ::tools::int128_t;
};
template <>
struct make_signed<const volatile ::tools::uint128_t> {
using type = const volatile ::tools::int128_t;
};
template <>
struct make_unsigned<::tools::int128_t> {
using type = ::tools::uint128_t;
};
template <>
struct make_unsigned<::tools::uint128_t> {
using type = ::tools::uint128_t;
};
template <>
struct make_unsigned<const ::tools::int128_t> {
using type = const ::tools::uint128_t;
};
template <>
struct make_unsigned<const ::tools::uint128_t> {
using type = const ::tools::uint128_t;
};
template <>
struct make_unsigned<volatile ::tools::int128_t> {
using type = volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<volatile ::tools::uint128_t> {
using type = volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<const volatile ::tools::int128_t> {
using type = const volatile ::tools::uint128_t;
};
template <>
struct make_unsigned<const volatile ::tools::uint128_t> {
using type = const volatile ::tools::uint128_t;
};
#if defined(__GLIBCXX__) && defined(__STRICT_ANSI__)
template <>
constexpr ::tools::uint128_t bit_ceil<::tools::uint128_t>(::tools::uint128_t x) noexcept {
if (x <= 1) return 1;
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x |= x >> 64;
return ++x;
}
template <>
constexpr ::tools::uint128_t bit_floor<::tools::uint128_t>(::tools::uint128_t x) noexcept {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x |= x >> 64;
return x & ~(x >> 1);
}
template <>
constexpr int bit_width<::tools::uint128_t>(::tools::uint128_t x) noexcept {
int w = 0;
if (x & UINT128_C(0xffffffffffffffff0000000000000000)) {
x >>= 64;
w += 64;
}
if (x & UINT128_C(0xffffffff00000000)) {
x >>= 32;
w += 32;
}
if (x & UINT128_C(0xffff0000)) {
x >>= 16;
w += 16;
}
if (x & UINT128_C(0xff00)) {
x >>= 8;
w += 8;
}
if (x & UINT128_C(0xf0)) {
x >>= 4;
w += 4;
}
if (x & UINT128_C(0xc)) {
x >>= 2;
w += 2;
}
if (x & UINT128_C(0x2)) {
x >>= 1;
w += 1;
}
w += x;
return w;
}
namespace detail {
namespace countr_zero {
template <::std::size_t N>
struct ntz_traits;
template <>
struct ntz_traits<128> {
using type = ::tools::uint128_t;
static constexpr int shift = 120;
static constexpr type magic = UINT128_C(0x01061438916347932a5cd9d3ead7b77f);
static constexpr int ntz_table[255] = {
128, 0, 1, -1, 2, -1, 8, -1, 3, -1, 15, -1, 9, -1, 22, -1,
4, -1, 29, -1, 16, -1, 36, -1, 10, -1, 43, -1, 23, -1, 50, -1,
5, -1, 33, -1, 30, -1, 57, -1, 17, -1, 64, -1, 37, -1, 71, -1,
11, -1, 60, -1, 44, -1, 78, -1, 24, -1, 85, -1, 51, -1, 92, -1,
-1, 6, -1, 20, -1, 34, -1, 48, 31, -1, -1, 69, 58, -1, -1, 90,
18, -1, 67, -1, 65, -1, 99, -1, 38, -1, 101, -1, 72, -1, 106, -1,
-1, 12, -1, 40, -1, 61, -1, 82, 45, -1, -1, 103, 79, -1, 113, -1,
-1, 25, -1, 74, 86, -1, -1, 116, -1, 52, -1, 108, -1, 93, -1, 120,
127, -1, -1, 7, -1, 14, -1, 21, -1, 28, -1, 35, -1, 42, -1, 49,
-1, 32, -1, 56, -1, 63, -1, 70, -1, 59, -1, 77, -1, 84, -1, 91,
-1, 19, -1, 47, -1, 68, -1, 89, -1, 66, -1, 98, -1, 100, -1, 105,
-1, 39, -1, 81, -1, 102, -1, 112, -1, 73, -1, 115, -1, 107, -1, 119,
126, -1, 13, -1, 27, -1, 41, -1, -1, 55, 62, -1, -1, 76, 83, -1,
-1, 46, -1, 88, -1, 97, -1, 104, -1, 80, -1, 111, -1, 114, -1, 118,
125, -1, 26, -1, 54, -1, 75, -1, -1, 87, 96, -1, -1, 110, -1, 117,
124, -1, 53, -1, -1, 95, 109, -1, 123, -1, 94, -1, 122, -1, 121
};
};
template <typename T>
constexpr int impl(const T x) noexcept {
using tr = ::tools::detail::countr_zero::ntz_traits<::std::numeric_limits<T>::digits>;
using type = typename tr::type;
return tr::ntz_table[static_cast<type>(tr::magic * static_cast<type>(x & -x)) >> tr::shift];
}
}
}
template <>
constexpr int countr_zero<::tools::uint128_t>(const ::tools::uint128_t x) noexcept {
return ::tools::detail::countr_zero::impl(x);
}
#endif
}
#line 5 "tools/uint128_t.hpp"
#line 5 "tools/prod_mod.hpp"
namespace tools {
template <typename T1, typename T2, typename T3>
constexpr T3 prod_mod(const T1 x, const T2 y, const T3 m) {
using u128 = ::tools::uint128_t;
u128 prod_mod = u128(x >= 0 ? x : -x) * u128(y >= 0 ? y : -y) % u128(m);
if ((x >= 0) ^ (y >= 0)) prod_mod = u128(m) - prod_mod;
return prod_mod;
}
}
#line 1 "tools/pow_mod.hpp"
#line 1 "tools/mod.hpp"
#line 7 "tools/mod.hpp"
namespace tools {
template <typename M, typename N> requires (
::tools::is_integral_v<M> && !::std::is_same_v<::std::remove_cv_t<M>, bool> &&
::tools::is_integral_v<N> && !::std::is_same_v<::std::remove_cv_t<N>, bool>)
constexpr ::std::common_type_t<M, N> mod(const M a, const N b) noexcept {
assert(b != 0);
using UM = ::std::make_unsigned_t<M>;
using UN = ::std::make_unsigned_t<N>;
const UM ua = a >= 0 ? a : static_cast<UM>(-(a + 1)) + 1;
const UN ub = b >= 0 ? b : static_cast<UN>(-(b + 1)) + 1;
auto r = ua % ub;
if (a < 0 && r > 0) {
r = ub - r;
}
return r;
}
}
#line 6 "tools/pow_mod.hpp"
namespace tools {
template <typename T1, typename T2, typename T3>
constexpr T3 pow_mod(const T1 x, T2 n, const T3 m) {
if (m == 1) return 0;
T3 r = 1;
T3 y = ::tools::mod(x, m);
while (n > 0) {
if ((n & 1) > 0) {
r = ::tools::prod_mod(r, y, m);
}
y = ::tools::prod_mod(y, y, m);
n /= 2;
}
return r;
}
}
#line 7 "tools/is_prime.hpp"
namespace tools {
constexpr bool is_prime(const unsigned long long n) {
constexpr ::std::array<unsigned long long, 7> bases = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
if (n <= 1) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
auto d = n - 1;
for (; d % 2 == 0; d /= 2);
for (const auto a : bases) {
if (a % n == 0) return true;
auto power = d;
auto target = ::tools::pow_mod(a, power, n);
bool is_composite = true;
if (target == 1) is_composite = false;
for (; is_composite && power != n - 1; power *= 2, target = ::tools::prod_mod(target, target, n)) {
if (target == n - 1) is_composite = false;
}
if (is_composite) {
return false;
}
}
return true;
}
}
#line 1 "tools/fact_mod_cache.hpp"
#line 8 "tools/fact_mod_cache.hpp"
#include <cmath>
#line 10 "tools/fact_mod_cache.hpp"
namespace tools {
template <class M>
class fact_mod_cache {
::std::vector<M> m_inv;
::std::vector<M> m_fact;
::std::vector<M> m_fact_inv;
public:
fact_mod_cache() : m_inv({M::raw(0), M::raw(1)}), m_fact({M::raw(1), M::raw(1)}), m_fact_inv({M::raw(1), M::raw(1)}) {
assert(::tools::is_prime(M::mod()));
}
explicit fact_mod_cache(const long long max) : fact_mod_cache() {
this->fact(::std::min<long long>(max, M::mod() - 1));
this->fact_inv(::std::min<long long>(max, M::mod() - 1));
}
M inv(const long long n) {
assert(n % M::mod() != 0);
const long long size = ::std::ssize(this->m_inv);
this->m_inv.resize(::std::clamp<long long>(::std::abs(n) + 1, size, M::mod()));
for (long long i = size; i < ::std::ssize(this->m_inv); ++i) {
this->m_inv[i] = -this->m_inv[M::mod() % i] * M::raw(M::mod() / i);
}
M result = this->m_inv[::std::abs(n) % M::mod()];
if (n < 0) result = -result;
return result;
}
M fact(const long long n) {
assert(n >= 0);
const long long size = ::std::ssize(this->m_fact);
this->m_fact.resize(::std::clamp<long long>(n + 1, size, M::mod()));
for (long long i = size; i < ::std::ssize(this->m_fact); ++i) {
this->m_fact[i] = this->m_fact[i - 1] * M::raw(i);
}
return n < M::mod() ? this->m_fact[n] : M::raw(0);
}
M fact_inv(const long long n) {
assert(0 <= n && n < M::mod());
const long long size = ::std::ssize(this->m_fact_inv);
this->m_fact_inv.resize(::std::max<long long>(size, n + 1));
this->inv(this->m_fact_inv.size() - 1);
for (long long i = size; i < ::std::ssize(this->m_fact_inv); ++i) {
this->m_fact_inv[i] = this->m_fact_inv[i - 1] * this->m_inv[i];
}
return this->m_fact_inv[n];
}
M binomial(long long n, long long r) {
if (r < 0) return M::raw(0);
if (0 <= n && n < r) return M::raw(0);
if (n < 0) return M(1 - ((r & 1) << 1)) * this->binomial(-n + r - 1, r);
this->fact(::std::min<long long>(n, M::mod() - 1));
this->fact_inv(::std::min<long long>(n, M::mod() - 1));
const auto c = [&](const long long nn, const long long rr) {
return 0 <= rr && rr <= nn ? this->m_fact[nn] * this->m_fact_inv[nn - rr] * this->m_fact_inv[rr] : M::raw(0);
};
M result(1);
while (n > 0 || r > 0) {
result *= c(n % M::mod(), r % M::mod());
n /= M::mod();
r /= M::mod();
}
return result;
}
M combination(const long long n, const long long r) {
if (!(0 <= r && r <= n)) return M::raw(0);
return this->binomial(n, r);
}
M permutation(const long long n, const long long r) {
if (!(0 <= r && r <= n)) return M::raw(0);
return this->binomial(n, r) * this->fact(r);
}
M combination_with_repetition(const long long n, const long long r) {
if (n < 0 || r < 0) return M::raw(0);
return this->binomial(n + r - 1, r);
}
};
}
#line 1 "tools/online_cumsum.hpp"
#line 1 "tools/group.hpp"
namespace tools {
namespace group {
template <typename G>
struct plus {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs + rhs;
}
static T e() {
return T(0);
}
static T inv(const T& v) {
return -v;
}
};
template <typename G>
struct multiplies {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs * rhs;
}
static T e() {
return T(1);
}
static T inv(const T& v) {
return e() / v;
}
};
template <typename G>
struct bit_xor {
using T = G;
static T op(const T& lhs, const T& rhs) {
return lhs ^ rhs;
}
static T e() {
return T(0);
}
static T inv(const T& v) {
return v;
}
};
}
}
#line 1 "tools/is_monoid.hpp"
#line 6 "tools/is_monoid.hpp"
namespace tools {
template <typename M, typename = void>
struct is_monoid : ::std::false_type {};
template <typename M>
struct is_monoid<M, ::std::enable_if_t<
::std::is_same_v<typename M::T, decltype(M::op(::std::declval<typename M::T>(), ::std::declval<typename M::T>()))> &&
::std::is_same_v<typename M::T, decltype(M::e())>
, void>> : ::std::true_type {};
template <typename M>
inline constexpr bool is_monoid_v = ::tools::is_monoid<M>::value;
}
#line 1 "tools/is_group.hpp"
#line 6 "tools/is_group.hpp"
namespace tools {
template <typename G, typename = void>
struct is_group : ::std::false_type {};
template <typename G>
struct is_group<G, ::std::enable_if_t<
::std::is_same_v<typename G::T, decltype(G::op(::std::declval<typename G::T>(), ::std::declval<typename G::T>()))> &&
::std::is_same_v<typename G::T, decltype(G::e())> &&
::std::is_same_v<typename G::T, decltype(G::inv(::std::declval<typename G::T>()))>
, void>> : ::std::true_type {};
template <typename G>
inline constexpr bool is_group_v = ::tools::is_group<G>::value;
}
#line 10 "tools/online_cumsum.hpp"
namespace tools {
template <typename X, bool Forward = true>
class online_cumsum {
using M = ::std::conditional_t<::tools::is_monoid_v<X>, X, ::tools::group::plus<X>>;
using T = typename M::T;
::std::vector<T> m_vector;
::std::vector<T> m_cumsum;
int m_processed;
public:
online_cumsum() : online_cumsum(0) {
}
online_cumsum(const int n) : m_vector(n, M::e()), m_cumsum(n + 1, M::e()), m_processed(Forward ? 0 : n) {
}
int size() const {
return this->m_vector.size();
}
T& operator[](const int i) {
assert(0 <= i && i < this->size());
return this->m_vector[i];
}
auto begin() { return this->m_vector.begin(); }
auto begin() const { return this->m_vector.begin(); }
auto cbegin() const { return this->m_vector.cbegin(); }
auto end() { return this->m_vector.end(); }
auto end() const { return this->m_vector.end(); }
auto cend() const { return this->m_vector.cend(); }
auto rbegin() { return this->m_vector.rbegin(); }
auto rbegin() const { return this->m_vector.rbegin(); }
auto crbegin() const { return this->m_vector.crbegin(); }
auto rend() { return this->m_vector.rend(); }
auto rend() const { return this->m_vector.rend(); }
auto crend() const { return this->m_vector.crend(); }
T prod(const int l, const int r) {
assert(0 <= l && l <= r && r <= this->size());
if constexpr (Forward) {
for (; this->m_processed < r; ++this->m_processed) {
this->m_cumsum[this->m_processed + 1] = M::op(this->m_cumsum[this->m_processed], this->m_vector[this->m_processed]);
}
if constexpr (::tools::is_group_v<M>) {
return M::op(M::inv(this->m_cumsum[l]), this->m_cumsum[r]);
} else {
assert(l == 0);
return this->m_cumsum[r];
}
} else {
for (; this->m_processed > l; --this->m_processed) {
this->m_cumsum[this->m_processed - 1] = M::op(this->m_vector[this->m_processed - 1], this->m_cumsum[this->m_processed]);
}
if constexpr (::tools::is_group_v<M>) {
return M::op(this->m_cumsum[l], M::inv(this->m_cumsum[r]));
} else {
assert(r == this->size());
return this->m_cumsum[l];
}
}
}
template <typename Y = X> requires (!::tools::is_monoid_v<Y>)
T sum(const int l, const int r) {
return this->prod(l, r);
}
};
}
#line 1 "tools/monoid.hpp"
#line 1 "tools/gcd.hpp"
#line 6 "tools/gcd.hpp"
namespace tools {
template <typename M, typename N>
constexpr ::std::common_type_t<M, N> gcd(const M m, const N n) {
return ::std::gcd(m, n);
}
}
#line 9 "tools/monoid.hpp"
namespace tools {
namespace monoid {
template <typename M, M ...dummy>
struct max;
template <typename M>
struct max<M> {
static_assert(::std::is_arithmetic_v<M>, "M must be a built-in arithmetic type.");
using T = M;
static T op(const T lhs, const T rhs) {
return ::std::max(lhs, rhs);
}
static T e() {
if constexpr (::std::is_integral_v<M>) {
return ::std::numeric_limits<M>::min();
} else {
return -::std::numeric_limits<M>::infinity();
}
}
};
template <typename M, M E>
struct max<M, E> {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
assert(E <= lhs);
assert(E <= rhs);
return ::std::max(lhs, rhs);
}
static T e() {
return E;
}
};
template <typename M, M ...dummy>
struct min;
template <typename M>
struct min<M> {
static_assert(::std::is_arithmetic_v<M>, "M must be a built-in arithmetic type.");
using T = M;
static T op(const T lhs, const T rhs) {
return ::std::min(lhs, rhs);
}
static T e() {
if constexpr (::std::is_integral_v<M>) {
return ::std::numeric_limits<M>::max();
} else {
return ::std::numeric_limits<M>::infinity();
}
}
};
template <typename M, M E>
struct min<M, E> {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
assert(lhs <= E);
assert(rhs <= E);
return ::std::min(lhs, rhs);
}
static T e() {
return E;
}
};
template <typename M>
struct multiplies {
private:
using VR = ::std::conditional_t<::std::is_arithmetic_v<M>, const M, const M&>;
public:
using T = M;
static T op(VR lhs, VR rhs) {
return lhs * rhs;
}
static T e() {
return T(1);
}
};
template <>
struct multiplies<bool> {
using T = bool;
static T op(const bool lhs, const bool rhs) {
return lhs && rhs;
}
static T e() {
return true;
}
};
template <typename M>
struct gcd {
private:
static_assert(!::std::is_arithmetic_v<M> || (::std::is_integral_v<M> && !::std::is_same_v<M, bool>), "If M is a built-in arithmetic type, it must be integral except for bool.");
using VR = ::std::conditional_t<::std::is_arithmetic_v<M>, const M, const M&>;
public:
using T = M;
static T op(VR lhs, VR rhs) {
return ::tools::gcd(lhs, rhs);
}
static T e() {
return T(0);
}
};
template <typename M, M E>
struct update {
static_assert(::std::is_integral_v<M>, "M must be a built-in integral type.");
using T = M;
static T op(const T lhs, const T rhs) {
return lhs == E ? rhs : lhs;
}
static T e() {
return E;
}
};
}
}
#line 1 "tools/convolution.hpp"
#line 5 "tools/convolution.hpp"
#include <complex>
#line 1 "lib/ac-library/atcoder/convolution.hpp"
#line 9 "lib/ac-library/atcoder/convolution.hpp"
#line 1 "lib/ac-library/atcoder/internal_bit.hpp"
#ifdef _MSC_VER
#include <intrin.h>
#endif
#if __cplusplus >= 202002L
#line 10 "lib/ac-library/atcoder/internal_bit.hpp"
#endif
namespace atcoder {
namespace internal {
#if __cplusplus >= 202002L
using std::bit_ceil;
#else
// @return same with std::bit::bit_ceil
unsigned int bit_ceil(unsigned int n) {
unsigned int x = 1;
while (x < (unsigned int)(n)) x *= 2;
return x;
}
#endif
// @param n `1 <= n`
// @return same with std::bit::countr_zero
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
// @param n `1 <= n`
// @return same with std::bit::countr_zero
constexpr int countr_zero_constexpr(unsigned int n) {
int x = 0;
while (!(n & (1 << x))) x++;
return x;
}
} // namespace internal
} // namespace atcoder
#line 12 "lib/ac-library/atcoder/convolution.hpp"
namespace atcoder {
namespace internal {
template <class mint,
int g = internal::primitive_root<mint::mod()>,
internal::is_static_modint_t<mint>* = nullptr>
struct fft_info {
static constexpr int rank2 = countr_zero_constexpr(mint::mod() - 1);
std::array<mint, rank2 + 1> root; // root[i]^(2^i) == 1
std::array<mint, rank2 + 1> iroot; // root[i] * iroot[i] == 1
std::array<mint, std::max(0, rank2 - 2 + 1)> rate2;
std::array<mint, std::max(0, rank2 - 2 + 1)> irate2;
std::array<mint, std::max(0, rank2 - 3 + 1)> rate3;
std::array<mint, std::max(0, rank2 - 3 + 1)> irate3;
fft_info() {
root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
iroot[rank2] = root[rank2].inv();
for (int i = rank2 - 1; i >= 0; i--) {
root[i] = root[i + 1] * root[i + 1];
iroot[i] = iroot[i + 1] * iroot[i + 1];
}
{
mint prod = 1, iprod = 1;
for (int i = 0; i <= rank2 - 2; i++) {
rate2[i] = root[i + 2] * prod;
irate2[i] = iroot[i + 2] * iprod;
prod *= iroot[i + 2];
iprod *= root[i + 2];
}
}
{
mint prod = 1, iprod = 1;
for (int i = 0; i <= rank2 - 3; i++) {
rate3[i] = root[i + 3] * prod;
irate3[i] = iroot[i + 3] * iprod;
prod *= iroot[i + 3];
iprod *= root[i + 3];
}
}
}
};
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly(std::vector<mint>& a) {
int n = int(a.size());
int h = internal::countr_zero((unsigned int)n);
static const fft_info<mint> info;
int len = 0; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
while (len < h) {
if (h - len == 1) {
int p = 1 << (h - len - 1);
mint rot = 1;
for (int s = 0; s < (1 << len); s++) {
int offset = s << (h - len);
for (int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p] * rot;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
if (s + 1 != (1 << len))
rot *= info.rate2[countr_zero(~(unsigned int)(s))];
}
len++;
} else {
// 4-base
int p = 1 << (h - len - 2);
mint rot = 1, imag = info.root[2];
for (int s = 0; s < (1 << len); s++) {
mint rot2 = rot * rot;
mint rot3 = rot2 * rot;
int offset = s << (h - len);
for (int i = 0; i < p; i++) {
auto mod2 = 1ULL * mint::mod() * mint::mod();
auto a0 = 1ULL * a[i + offset].val();
auto a1 = 1ULL * a[i + offset + p].val() * rot.val();
auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val();
auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val();
auto a1na3imag =
1ULL * mint(a1 + mod2 - a3).val() * imag.val();
auto na2 = mod2 - a2;
a[i + offset] = a0 + a2 + a1 + a3;
a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
}
if (s + 1 != (1 << len))
rot *= info.rate3[countr_zero(~(unsigned int)(s))];
}
len += 2;
}
}
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
void butterfly_inv(std::vector<mint>& a) {
int n = int(a.size());
int h = internal::countr_zero((unsigned int)n);
static const fft_info<mint> info;
int len = h; // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
while (len) {
if (len == 1) {
int p = 1 << (h - len);
mint irot = 1;
for (int s = 0; s < (1 << (len - 1)); s++) {
int offset = s << (h - len + 1);
for (int i = 0; i < p; i++) {
auto l = a[i + offset];
auto r = a[i + offset + p];
a[i + offset] = l + r;
a[i + offset + p] =
(unsigned long long)(mint::mod() + l.val() - r.val()) *
irot.val();
;
}
if (s + 1 != (1 << (len - 1)))
irot *= info.irate2[countr_zero(~(unsigned int)(s))];
}
len--;
} else {
// 4-base
int p = 1 << (h - len);
mint irot = 1, iimag = info.iroot[2];
for (int s = 0; s < (1 << (len - 2)); s++) {
mint irot2 = irot * irot;
mint irot3 = irot2 * irot;
int offset = s << (h - len + 2);
for (int i = 0; i < p; i++) {
auto a0 = 1ULL * a[i + offset + 0 * p].val();
auto a1 = 1ULL * a[i + offset + 1 * p].val();
auto a2 = 1ULL * a[i + offset + 2 * p].val();
auto a3 = 1ULL * a[i + offset + 3 * p].val();
auto a2na3iimag =
1ULL *
mint((mint::mod() + a2 - a3) * iimag.val()).val();
a[i + offset] = a0 + a1 + a2 + a3;
a[i + offset + 1 * p] =
(a0 + (mint::mod() - a1) + a2na3iimag) * irot.val();
a[i + offset + 2 * p] =
(a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) *
irot2.val();
a[i + offset + 3 * p] =
(a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) *
irot3.val();
}
if (s + 1 != (1 << (len - 2)))
irot *= info.irate3[countr_zero(~(unsigned int)(s))];
}
len -= 2;
}
}
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_naive(const std::vector<mint>& a,
const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
std::vector<mint> ans(n + m - 1);
if (n < m) {
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
ans[i + j] += a[i] * b[j];
}
}
} else {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i + j] += a[i] * b[j];
}
}
}
return ans;
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution_fft(std::vector<mint> a, std::vector<mint> b) {
int n = int(a.size()), m = int(b.size());
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
a.resize(z);
internal::butterfly(a);
b.resize(z);
internal::butterfly(b);
for (int i = 0; i < z; i++) {
a[i] *= b[i];
}
internal::butterfly_inv(a);
a.resize(n + m - 1);
mint iz = mint(z).inv();
for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
return a;
}
} // namespace internal
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(std::vector<mint>&& a, std::vector<mint>&& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
if (std::min(n, m) <= 60) return convolution_naive(a, b);
return internal::convolution_fft(a, b);
}
template <class mint, internal::is_static_modint_t<mint>* = nullptr>
std::vector<mint> convolution(const std::vector<mint>& a,
const std::vector<mint>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
if (std::min(n, m) <= 60) return convolution_naive(a, b);
return internal::convolution_fft(a, b);
}
template <unsigned int mod = 998244353,
class T,
std::enable_if_t<internal::is_integral<T>::value>* = nullptr>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
using mint = static_modint<mod>;
int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
assert((mint::mod() - 1) % z == 0);
std::vector<mint> a2(n), b2(m);
for (int i = 0; i < n; i++) {
a2[i] = mint(a[i]);
}
for (int i = 0; i < m; i++) {
b2[i] = mint(b[i]);
}
auto c2 = convolution(std::move(a2), std::move(b2));
std::vector<T> c(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
c[i] = c2[i].val();
}
return c;
}
std::vector<long long> convolution_ll(const std::vector<long long>& a,
const std::vector<long long>& b) {
int n = int(a.size()), m = int(b.size());
if (!n || !m) return {};
static constexpr unsigned long long MOD1 = 754974721; // 2^24
static constexpr unsigned long long MOD2 = 167772161; // 2^25
static constexpr unsigned long long MOD3 = 469762049; // 2^26
static constexpr unsigned long long M2M3 = MOD2 * MOD3;
static constexpr unsigned long long M1M3 = MOD1 * MOD3;
static constexpr unsigned long long M1M2 = MOD1 * MOD2;
static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;
static constexpr unsigned long long i1 =
internal::inv_gcd(MOD2 * MOD3, MOD1).second;
static constexpr unsigned long long i2 =
internal::inv_gcd(MOD1 * MOD3, MOD2).second;
static constexpr unsigned long long i3 =
internal::inv_gcd(MOD1 * MOD2, MOD3).second;
static constexpr int MAX_AB_BIT = 24;
static_assert(MOD1 % (1ull << MAX_AB_BIT) == 1, "MOD1 isn't enough to support an array length of 2^24.");
static_assert(MOD2 % (1ull << MAX_AB_BIT) == 1, "MOD2 isn't enough to support an array length of 2^24.");
static_assert(MOD3 % (1ull << MAX_AB_BIT) == 1, "MOD3 isn't enough to support an array length of 2^24.");
assert(n + m - 1 <= (1 << MAX_AB_BIT));
auto c1 = convolution<MOD1>(a, b);
auto c2 = convolution<MOD2>(a, b);
auto c3 = convolution<MOD3>(a, b);
std::vector<long long> c(n + m - 1);
for (int i = 0; i < n + m - 1; i++) {
unsigned long long x = 0;
x += (c1[i] * i1) % MOD1 * M2M3;
x += (c2[i] * i2) % MOD2 * M1M3;
x += (c3[i] * i3) % MOD3 * M1M2;
// B = 2^63, -B <= x, r(real value) < B
// (x, x - M, x - 2M, or x - 3M) = r (mod 2B)
// r = c1[i] (mod MOD1)
// focus on MOD1
// r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B)
// r = x,
// x - M' + (0 or 2B),
// x - 2M' + (0, 2B or 4B),
// x - 3M' + (0, 2B, 4B or 6B) (without mod!)
// (r - x) = 0, (0)
// - M' + (0 or 2B), (1)
// -2M' + (0 or 2B or 4B), (2)
// -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1)
// we checked that
// ((1) mod MOD1) mod 5 = 2
// ((2) mod MOD1) mod 5 = 3
// ((3) mod MOD1) mod 5 = 4
long long diff =
c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
if (diff < 0) diff += MOD1;
static constexpr unsigned long long offset[5] = {
0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
x -= offset[diff % 5];
c[i] = x;
}
return c;
}
} // namespace atcoder
#line 1 "tools/pow2.hpp"
#line 6 "tools/pow2.hpp"
namespace tools {
template <typename T, typename ::std::enable_if<::std::is_unsigned<T>::value, ::std::nullptr_t>::type = nullptr>
constexpr T pow2(const T x) {
return static_cast<T>(1) << x;
}
template <typename T, typename ::std::enable_if<::std::is_signed<T>::value, ::std::nullptr_t>::type = nullptr>
constexpr T pow2(const T x) {
return static_cast<T>(static_cast<typename ::std::make_unsigned<T>::type>(1) << static_cast<typename ::std::make_unsigned<T>::type>(x));
}
}
#line 1 "tools/ceil_log2.hpp"
#line 6 "tools/ceil_log2.hpp"
namespace tools {
template <typename T>
constexpr T ceil_log2(T x) noexcept {
assert(x > 0);
return ::tools::bit_width(x - 1);
}
}
#line 1 "tools/garner3.hpp"
#line 7 "tools/garner3.hpp"
namespace tools {
template <typename M, typename M1, typename M2, typename M3>
M garner3(const M1& a, const M2& b, const M3& c, const M m) {
using ull = unsigned long long;
static const M2 m1_inv_mod_m2 = M2::raw(M1::mod()).inv();
static const M3 m1_m2_inv_mod_m3 = (M3::raw(M1::mod()) * M3::raw(M2::mod())).inv();
static const auto plus_mod = [](ull x, const ull y, const ull mod) {
assert(x < mod);
assert(y < mod);
x += y;
if (x >= mod) x -= mod;
return x;
};
assert(m >= 1);
assert(M1::mod() < M2::mod());
assert(M2::mod() < M3::mod());
assert(::tools::is_prime(M1::mod()));
assert(::tools::is_prime(M2::mod()));
assert(::tools::is_prime(M3::mod()));
// t1 = (b - a) / M1; (mod M2)
// t2 = (c - a - t1 * M1) / M1 / M2; (mod M3)
// return a + t1 * M1 + t2 * M1 * M2; (mod m)
const M2 t1 = (b - M2::raw(a.val())) * m1_inv_mod_m2;
const M3 t2 = (c - M3::raw(a.val()) - M3::raw(t1.val()) * M3::raw(M1::mod())) * m1_m2_inv_mod_m3;
ull r = ::tools::prod_mod(t2.val(), ull(M1::mod()) * ull(M2::mod()), m);
assert(r < ull(m));
r = plus_mod(r, ull(t1.val()) * ull(M1::mod()) % m, m);
assert(r < ull(m));
r = plus_mod(r, a.val() % m, m);
assert(r < ull(m));
return r;
}
}
#line 22 "tools/convolution.hpp"
namespace tools {
namespace detail {
namespace convolution {
template <typename T, typename = void>
struct make_complex {
using type = T;
};
template <typename T>
struct make_complex<T, ::std::enable_if_t<::std::is_floating_point_v<T>, void>> {
using type = ::std::complex<T>;
};
template <typename T>
using make_complex_t = typename ::tools::detail::convolution::make_complex<T>::type;
template <typename AG, typename MM, typename InputIterator1, typename InputIterator2, typename OutputIterator>
void naive(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
static_assert(::std::is_same_v<typename AG::T, typename MM::T>);
assert(a_begin != a_end);
assert(b_begin != b_end);
using T = typename AG::T;
const auto n = ::std::distance(a_begin, a_end);
const auto m = ::std::distance(b_begin, b_end);
::std::vector<T> c(n + m - 1, AG::e());
if (n < m) {
auto c_begin = c.begin();
for (auto b_it = b_begin; b_it != b_end; ++b_it, ++c_begin) {
auto c_it = c_begin;
for (auto a_it = a_begin; a_it != a_end; ++a_it, ++c_it) {
*c_it = AG::op(*c_it, MM::op(*a_it, *b_it));
}
}
} else {
auto c_begin = c.begin();
for (auto a_it = a_begin; a_it != a_end; ++a_it, ++c_begin) {
auto c_it = c_begin;
for (auto b_it = b_begin; b_it != b_end; ++b_it, ++c_it) {
*c_it = AG::op(*c_it, MM::op(*a_it, *b_it));
}
}
}
::std::move(c.begin(), c.end(), result);
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void fft(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
using C = ::tools::detail::convolution::make_complex_t<T>;
static_assert(::std::is_same_v<C, ::std::complex<float>> || ::std::is_same_v<C, ::std::complex<double>> || ::std::is_same_v<C, ::std::complex<long double>>);
using R = typename C::value_type;
assert(a_begin != a_end);
assert(b_begin != b_end);
::std::vector<C> a, b;
if constexpr (::std::is_same_v<T, R>) {
for (auto it = a_begin; it != a_end; ++it) {
a.emplace_back(*it, 0);
}
for (auto it = b_begin; it != b_end; ++it) {
b.emplace_back(*it, 0);
}
} else if constexpr (::std::is_same_v<T, C>) {
a.assign(a_begin, a_end);
b.assign(b_begin, b_end);
}
const auto n = a.size() + b.size() - 1;
const auto z = ::tools::pow2(::tools::ceil_log2(n));
a.resize(z);
b.resize(z);
::std::vector<C> pow_root;
pow_root.reserve(z);
pow_root.emplace_back(1, 0);
if (z > 1) pow_root.push_back(::std::polar<R>(1, R(2) * ::std::acos(R(-1)) / z));
for (::std::size_t p = 2; p < z; p *= 2) {
pow_root.push_back(pow_root[p / 2] * pow_root[p / 2]);
for (::std::size_t i = p + 1; i < p * 2; ++i) {
pow_root.push_back(pow_root[p] * pow_root[i - p]);
}
}
const auto butterfly = [&](::std::vector<C>& f) {
::std::vector<C> prev(z);
for (::std::size_t p = z / 2; p >= 1; p /= 2) {
prev.swap(f);
for (::std::size_t qp = 0; qp < z; qp += p) {
for (::std::size_t r = 0; r < p; ++r) {
f[qp + r] = prev[qp * 2 % z + r] + pow_root[qp] * prev[qp * 2 % z + p + r];
}
}
}
};
butterfly(a);
butterfly(b);
for (::std::size_t i = 0; i < z; ++i) {
a[i] *= b[i];
}
::std::reverse(::std::next(pow_root.begin()), pow_root.end());
butterfly(a);
for (::std::size_t i = 0; i < n; ++i) {
if constexpr (::std::is_same_v<T, R>) {
*result = a[i].real() / z;
} else {
*result = a[i] / z;
}
++result;
}
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void ntt(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using M = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<M, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
static_assert(::atcoder::internal::is_static_modint<M>::value);
static_assert(2 <= M::mod() && M::mod() <= 2000000000);
static_assert(::tools::is_prime(M::mod()));
assert(a_begin != a_end);
assert(b_begin != b_end);
::std::vector<M> a(a_begin, a_end);
::std::vector<M> b(b_begin, b_end);
const auto n = a.size();
const auto m = b.size();
const auto z = ::tools::pow2(::tools::ceil_log2(n + m - 1));
assert((M::mod() - 1) % z == 0);
if (n == m && 4 * n == z + 4) {
const auto afbf = a.front() * b.front();
const auto abbb = a.back() * b.back();
a.resize(z / 2);
::atcoder::internal::butterfly(a);
b.resize(z / 2);
::atcoder::internal::butterfly(b);
for (::std::size_t i = 0; i < z / 2; ++i) {
a[i] *= b[i];
}
::atcoder::internal::butterfly_inv(a);
const auto iz = M(z / 2).inv();
*result = afbf;
++result;
for (::std::size_t i = 1; i < n + m - 2; ++i) {
*result = a[i] * iz;
++result;
}
*result = abbb;
++result;
} else {
a.resize(z);
::atcoder::internal::butterfly(a);
b.resize(z);
::atcoder::internal::butterfly(b);
for (::std::size_t i = 0; i < z; ++i) {
a[i] *= b[i];
}
::atcoder::internal::butterfly_inv(a);
const auto iz = M(z).inv();
for (::std::size_t i = 0; i < n + m - 1; ++i) {
*result = a[i] * iz;
++result;
}
}
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void ntt_and_garner(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using M = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<M, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
using M1 = ::atcoder::static_modint<1107296257>; // 33 * 2^25 + 1
using M2 = ::atcoder::static_modint<1711276033>; // 51 * 2^25 + 1
using M3 = ::atcoder::static_modint<1811939329>; // 27 * 2^26 + 1
static_assert(::atcoder::internal::is_static_modint<M>::value || ::atcoder::internal::is_dynamic_modint<M>::value);
assert(a_begin != a_end);
assert(b_begin != b_end);
const auto n = ::std::distance(a_begin, a_end);
const auto m = ::std::distance(b_begin, b_end);
const auto z = ::tools::pow2(::tools::ceil_log2(n + m - 1));
assert((M1::mod() - 1) % z == 0);
assert((M2::mod() - 1) % z == 0);
assert((M3::mod() - 1) % z == 0);
// No need for the following assertion because the condition always holds.
// assert(std::min(a.size(), b.size()) * tools::square(M::mod() - 1) < M1::mod() * M2::mod() * M3::mod());
::std::vector<M1> c1;
c1.reserve(n + m - 1);
{
::std::vector<M1> a1;
a1.reserve(n);
for (auto it = a_begin; it != a_end; ++it) {
a1.emplace_back(it->val());
}
::std::vector<M1> b1;
b1.reserve(m);
for (auto it = b_begin; it != b_end; ++it) {
b1.emplace_back(it->val());
}
::tools::detail::convolution::ntt(a1.begin(), a1.end(), b1.begin(), b1.end(), ::std::back_inserter(c1));
}
::std::vector<M2> c2;
c2.reserve(n + m - 1);
{
::std::vector<M2> a2;
a2.reserve(n);
for (auto it = a_begin; it != a_end; ++it) {
a2.emplace_back(it->val());
}
::std::vector<M2> b2;
b2.reserve(m);
for (auto it = b_begin; it != b_end; ++it) {
b2.emplace_back(it->val());
}
::tools::detail::convolution::ntt(a2.begin(), a2.end(), b2.begin(), b2.end(), ::std::back_inserter(c2));
}
::std::vector<M3> c3;
c3.reserve(n + m - 1);
{
::std::vector<M3> a3;
a3.reserve(n);
for (auto it = a_begin; it != a_end; ++it) {
a3.emplace_back(it->val());
}
::std::vector<M3> b3;
b3.reserve(m);
for (auto it = b_begin; it != b_end; ++it) {
b3.emplace_back(it->val());
}
::tools::detail::convolution::ntt(a3.begin(), a3.end(), b3.begin(), b3.end(), ::std::back_inserter(c3));
}
for (::std::size_t i = 0; i < c1.size(); ++i) {
*result = M::raw(::tools::garner3(c1[i], c2[i], c3[i], M::mod()));
++result;
}
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void ntt_and_garner_for_ll(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using Z = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<Z, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
using ll = long long;
static_assert(::std::is_integral_v<Z>);
assert(a_begin != a_end);
assert(b_begin != b_end);
const auto n = ::std::distance(a_begin, a_end);
const auto m = ::std::distance(b_begin, b_end);
assert(n + m - 1 <= ::tools::pow2(24));
::std::vector<ll> a, b;
a.reserve(n);
b.reserve(m);
::std::copy(a_begin, a_end, ::std::back_inserter(a));
::std::copy(b_begin, b_end, ::std::back_inserter(b));
for (const auto c_i : ::atcoder::convolution_ll(a, b)) {
*result = c_i;
++result;
}
}
}
}
template <typename AG, typename MM, typename InputIterator1, typename InputIterator2, typename OutputIterator>
void convolution(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, OutputIterator result) {
using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
if (a_begin == a_end || b_begin == b_end) return;
const auto n = ::std::distance(a_begin, a_end);
const auto m = ::std::distance(b_begin, b_end);
if (::std::min(n, m) <= 60) {
::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
return;
}
if constexpr (::std::is_same_v<AG, ::tools::group::plus<T>> && (::std::is_same_v<MM, ::tools::monoid::multiplies<T>> || ::std::is_same_v<MM, ::tools::group::multiplies<T>>)) {
if constexpr (::std::is_floating_point_v<T> || ::std::is_same_v<T, ::std::complex<float>> || ::std::is_same_v<T, ::std::complex<double>> || ::std::is_same_v<T, ::std::complex<long double>>) {
::tools::detail::convolution::fft(a_begin, a_end, b_begin, b_end, result);
} else if constexpr (::std::is_integral_v<T>) {
::tools::detail::convolution::ntt_and_garner_for_ll(a_begin, a_end, b_begin, b_end, result);
} else if constexpr (::atcoder::internal::is_static_modint<T>::value || ::atcoder::internal::is_dynamic_modint<T>::value) {
if constexpr (::atcoder::internal::is_static_modint<T>::value && T::mod() <= 2000000000 && ::tools::is_prime(T::mod())) {
if ((T::mod() - 1) % ::tools::pow2(::tools::ceil_log2(n + m - 1)) == 0) {
::tools::detail::convolution::ntt(a_begin, a_end, b_begin, b_end, result);
} else {
::tools::detail::convolution::ntt_and_garner(a_begin, a_end, b_begin, b_end, result);
}
} else {
::tools::detail::convolution::ntt_and_garner(a_begin, a_end, b_begin, b_end, result);
}
} else {
::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
}
} else {
::tools::detail::convolution::naive<AG, MM>(a_begin, a_end, b_begin, b_end, result);
}
}
template <typename InputIterator1, typename InputIterator2, typename OutputIterator>
void convolution(const InputIterator1 a_begin, const InputIterator1 a_end, const InputIterator2 b_begin, const InputIterator2 b_end, const OutputIterator result) {
using T = ::std::decay_t<decltype(*::std::declval<InputIterator1>())>;
static_assert(::std::is_same_v<T, ::std::decay_t<decltype(*::std::declval<InputIterator2>())>>);
::tools::convolution<::tools::group::plus<T>, ::tools::monoid::multiplies<T>>(a_begin, a_end, b_begin, b_end, result);
}
}
#line 16 "tools/sample_point_shift.hpp"
namespace tools {
template <typename RandomAccessIterator>
::std::enable_if_t<
::std::is_base_of_v<
::std::random_access_iterator_tag,
typename ::std::iterator_traits<RandomAccessIterator>::iterator_category
>,
typename ::std::iterator_traits<RandomAccessIterator>::value_type
> sample_point_shift(const RandomAccessIterator begin, const RandomAccessIterator end, const typename ::std::iterator_traits<RandomAccessIterator>::value_type c) {
using T = typename ::std::iterator_traits<RandomAccessIterator>::value_type;
assert(::tools::is_prime(T::mod()));
const int N = ::std::distance(begin, end);
assert(1 <= N && N <= T::mod());
::tools::fact_mod_cache<T> cache;
const ::std::array<T, 2> minus_1_pow = {T(1), T(-1)};
::tools::online_cumsum<::tools::monoid::multiplies<T>, true> nl(N);
::tools::online_cumsum<::tools::monoid::multiplies<T>, false> nr(N);
{
T last = c;
for (int i = 0; i < N; ++i, --last) {
nl[i] = nr[i] = last;
}
}
T answer(0);
for (int i = 0; i < N; ++i) {
answer += nl.prod(0, i) * nr.prod(i + 1, N) * minus_1_pow[(N - 1 - i) & 1] * cache.fact_inv(N - 1 - i) * cache.fact_inv(i) * begin[i];
}
return answer;
}
template <typename InputIterator>
::std::enable_if_t<
!::std::is_base_of_v<
::std::random_access_iterator_tag,
typename ::std::iterator_traits<InputIterator>::iterator_category
>,
typename ::std::iterator_traits<InputIterator>::value_type
> sample_point_shift(const InputIterator begin, const InputIterator end, const typename ::std::iterator_traits<InputIterator>::value_type c) {
using T = typename ::std::iterator_traits<InputIterator>::value_type;
const ::std::vector<T> samples(begin, end);
return ::tools::sample_point_shift(samples.begin(), samples.end(), c);
}
template <typename T>
T sample_point_shift(const ::std::initializer_list<T> il, const T c) {
return ::tools::sample_point_shift(il.begin(), il.end(), c);
}
template <typename RandomAccessIterator, typename OutputIterator>
::std::enable_if_t<
::std::is_base_of_v<
::std::random_access_iterator_tag,
typename ::std::iterator_traits<RandomAccessIterator>::iterator_category
>,
void
> sample_point_shift(const RandomAccessIterator begin, const RandomAccessIterator end, const typename ::std::iterator_traits<RandomAccessIterator>::value_type c, const int M, OutputIterator result) {
using T = typename ::std::iterator_traits<RandomAccessIterator>::value_type;
assert(::tools::is_prime(T::mod()));
const int N = ::std::distance(begin, end);
assert(1 <= N && N <= T::mod());
assert(0 <= M);
if (M == 1) {
result = ::tools::sample_point_shift(begin, end, c);
++result;
}
if (M <= 1) return;
::tools::fact_mod_cache<T> cache;
const ::std::array<T, 2> minus_1_pow = {T(1), T(-1)};
::std::vector<T> c1;
{
::std::vector<T> a1(N);
for (int i = 0; i < N; ++i) {
a1[i] = begin[i] * cache.fact_inv(i);
}
::std::vector<T> b1(N);
for (int i = 0; i < N; ++i) {
b1[i] = minus_1_pow[i & 1] * cache.fact_inv(i);
}
::tools::convolution(a1.begin(), a1.end(), b1.begin(), b1.end(), ::std::back_inserter(c1));
c1.resize(N);
}
::std::vector<T> c2;
{
::std::vector<T> a2(N);
for (int i = 0; i < N; ++i) {
a2[i] = c1[N - 1 - i] * cache.fact(N - 1 - i);
}
::std::vector<T> b2(N);
b2[0] = T(1);
T b = c;
for (int i = 1; i < N; ++i, --b) {
b2[i] = b2[i - 1] * b;
}
for (int i = 0; i < N; ++i) {
b2[i] *= cache.fact_inv(i);
}
::tools::convolution(a2.begin(), a2.end(), b2.begin(), b2.end(), ::std::back_inserter(c2));
c2.resize(N);
::std::reverse(c2.begin(), c2.end());
for (int i = 0; i < N; ++i) {
c2[i] *= cache.fact_inv(i);
}
}
::std::vector<T> c3;
const int m = ::std::min(M, T::mod());
{
const auto& a3 = c2;
::std::vector<T> b3(m);
for (int i = 0; i < m; ++i) {
b3[i] = cache.fact_inv(i);
}
::tools::convolution(a3.begin(), a3.end(), b3.begin(), b3.end(), ::std::back_inserter(c3));
c3.resize(m);
for (int i = 0; i < m; ++i) {
c3[i] *= cache.fact(i);
}
}
c3.resize(M);
for (int i = m; i < M; ++i) {
c3[i] = c3[i % T::mod()];
}
::std::copy(c3.begin(), c3.end(), result);
}
template <typename InputIterator, typename OutputIterator>
::std::enable_if_t<
!::std::is_base_of_v<
::std::random_access_iterator_tag,
typename ::std::iterator_traits<InputIterator>::iterator_category
>,
void
> sample_point_shift(const InputIterator begin, const InputIterator end, const typename ::std::iterator_traits<InputIterator>::value_type c, const int M, const OutputIterator result) {
using T = typename ::std::iterator_traits<InputIterator>::value_type;
const ::std::vector<T> samples(begin, end);
::tools::sample_point_shift(samples.begin(), samples.end(), c, M, result);
}
template <typename InputIterator>
::std::vector<typename ::std::iterator_traits<InputIterator>::value_type>
sample_point_shift(const InputIterator begin, const InputIterator end, const typename ::std::iterator_traits<InputIterator>::value_type c, const int M) {
using T = typename ::std::iterator_traits<InputIterator>::value_type;
::std::vector<T> res;
::tools::sample_point_shift(begin, end, c, M, ::std::back_inserter(res));
return res;
}
template <typename T>
::std::vector<T> sample_point_shift(const ::std::initializer_list<T> il, const T c, const int M) {
::std::vector<T> res;
::tools::sample_point_shift(il.begin(), il.end(), c, M, ::std::back_inserter(res));
return res;
}
}
#line 1 "tools/join.hpp"
#line 5 "tools/join.hpp"
#include <sstream>
namespace tools {
template <::std::ranges::range R, typename T>
::std::string join(R&& e, const T& d) {
::std::ostringstream ss;
auto it = ::std::ranges::begin(e);
const auto end = ::std::ranges::end(e);
if (it != end) {
ss << *it;
for (++it; it != end; ++it) {
ss << d << *it;
}
}
return ss.str();
}
}
#line 9 "tests/sample_point_shift.test.cpp"
using mint = atcoder::modint998244353;
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int N, M;
std::cin >> N >> M;
mint c;
{
int c_int;
std::cin >> c_int;
c = mint::raw(c_int);
}
std::vector<mint> f(N);
for (int i = 0; i < N; ++i) {
int f_i;
std::cin >> f_i;
f[i] = mint::raw(f_i);
}
const auto g = tools::sample_point_shift(f.begin(), f.end(), c, M);
std::cout << tools::join(g | std::views::transform([](const auto g_i) { return g_i.val(); }), " ") << '\n';
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++ | N_1_00 |
![]() |
46 ms | 14 MB |
g++ | c_0_00 |
![]() |
274 ms | 38 MB |
g++ | example_00 |
![]() |
5 ms | 4 MB |
g++ | example_01 |
![]() |
5 ms | 4 MB |
g++ | max_random_00 |
![]() |
298 ms | 40 MB |
g++ | max_random_01 |
![]() |
298 ms | 40 MB |
g++ | max_random_02 |
![]() |
299 ms | 40 MB |
g++ | max_random_03 |
![]() |
292 ms | 40 MB |
g++ | medium_random_00 |
![]() |
12 ms | 4 MB |
g++ | medium_random_01 |
![]() |
12 ms | 4 MB |
g++ | medium_random_02 |
![]() |
12 ms | 4 MB |
g++ | medium_random_03 |
![]() |
12 ms | 4 MB |
g++ | small_random_00 |
![]() |
5 ms | 4 MB |
g++ | small_random_01 |
![]() |
5 ms | 4 MB |
g++ | small_random_02 |
![]() |
4 ms | 4 MB |
g++ | small_random_03 |
![]() |
5 ms | 4 MB |
g++ | type0_random_00 |
![]() |
216 ms | 32 MB |
g++ | type0_random_01 |
![]() |
224 ms | 35 MB |
g++ | type0_random_02 |
![]() |
32 ms | 7 MB |
g++ | type0_random_03 |
![]() |
255 ms | 36 MB |
g++ | type1_random_00 |
![]() |
277 ms | 38 MB |
g++ | type1_random_01 |
![]() |
264 ms | 40 MB |
g++ | type1_random_02 |
![]() |
37 ms | 7 MB |
g++ | type1_random_03 |
![]() |
282 ms | 38 MB |
g++ | type2_random_00 |
![]() |
299 ms | 36 MB |
g++ | type2_random_01 |
![]() |
281 ms | 39 MB |
g++ | type2_random_02 |
![]() |
94 ms | 17 MB |
g++ | type2_random_03 |
![]() |
216 ms | 35 MB |
g++ | type3_random_00 |
![]() |
271 ms | 36 MB |
g++ | type3_random_01 |
![]() |
281 ms | 39 MB |
g++ | type3_random_02 |
![]() |
94 ms | 17 MB |
g++ | type3_random_03 |
![]() |
218 ms | 34 MB |