1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
| #include <iostream> #include <vector> #include <array> #include <type_traits> #include <complex> #include <concepts>
namespace zmile {
template<class T> concept GroupElement = std::is_arithmetic_v<T> || requires(T x) { x = 1; x* x; };
template<class T> concept BinaryPowerNumber = requires(T x) { x >>= 1; x & 1; };
template<GroupElement ValueT, BinaryPowerNumber PowerT> constexpr ValueT qpow(ValueT a, PowerT b, ValueT unit = 1) { for (; b; b >>= 1, a = a * a) if (b & 1) unit = unit * a; return unit; }
template <class ValueT = int, ValueT M = 998244353> requires(std::is_integral_v<ValueT>&& M > 0 && requires(ValueT x) { x = M; }) class ModInt { using ThisT = ModInt<ValueT, M>; ValueT value; public: using value_t = ValueT;
constexpr ModInt() : value(0) { } constexpr ModInt(ValueT value) : value((value % M + M) % M) { }
inline constexpr explicit operator bool() const { return value; }
inline constexpr ThisT operator-(void) const { return (M - value) % M; }
inline constexpr ThisT operator+(const ThisT& rhs) const { return (value + rhs.value) % M; } inline constexpr ThisT operator-(const ThisT& rhs) const { return *this + -rhs; } inline constexpr ThisT operator*(const ThisT& rhs) const { return ValueT(1ull * value * rhs.value % M); } inline constexpr ThisT operator/(const ThisT& rhs) const { return *this * qpow(rhs, M-2); }
inline constexpr ThisT& operator+=(const ThisT& rhs) { return *this = *this + rhs; } inline constexpr ThisT& operator-=(const ThisT& rhs) { return *this = *this - rhs; } inline constexpr ThisT& operator*=(const ThisT& rhs) { return *this = *this * rhs; } inline constexpr ThisT& operator/=(const ThisT& rhs) { return *this = *this / rhs; }
inline constexpr bool operator==(const ThisT& rhs) const { return value == rhs.get_value(); } inline constexpr bool operator==(const ValueT& rhs) const { return value == rhs; }
inline constexpr ThisT get_inv() const { return ThisT(1) / value; } inline constexpr ValueT get_value() const { return value; } };
template <class ValueT, ValueT M> requires(std::is_integral_v<ValueT>&& M > 0 && requires(ValueT x) { x = M; }) std::ostream& operator<<(std::ostream& os, ModInt<ValueT, M> val) { return os << val.get_value(); }
template <class ValueT, ValueT M> requires(std::is_integral_v<ValueT>&& M > 0 && requires(ValueT x) { x = M; }) std::istream& operator>>(std::istream& is, ModInt<ValueT, M>& val) { static ValueT temp_val; is >> temp_val; val = temp_val; return is; }
template<auto mod> requires(std::is_integral_v<decltype(mod)> && requires() { mod % mod; mod - mod; mod / mod; qpow(mod, mod); }) constexpr decltype(mod) find_primitive_root_internal() { using ValueT = decltype(mod); constexpr int MAX_PRIME_FACTOR_COUNT = 17; std::array<ValueT, MAX_PRIME_FACTOR_COUNT> fac{ 0 }; auto vm = mod - 1; int pos = 0; for (int x = 2; x * x <= vm; x++) { if (vm % x == 0) { fac[pos++] = x; while (vm % x == 0) vm /= x; } } if (vm != 1) fac[pos++] = vm;
for (auto ans = 2; ans < mod; ans++) { bool failed = false; for (auto x : fac) { if (x == 0 || x == 1) break; if (qpow<class ModInt<ValueT, mod>>(ans, (mod - 1) / x) == 1) { failed = true; break; } } if (!failed) return ans; }
return 0; }
template <auto mod> constexpr auto find_primitive_root() { constexpr auto rt = find_primitive_root_internal<mod>(); static_assert(rt, "FATAL ERR: the primitive root doesn't exists!"); return rt; }
inline constexpr static auto get_max_valid_length(auto M) { auto Mt = M; size_t cnt2 = 0; while (!(Mt & 1)) Mt >>= 1, cnt2++; return 1ull << cnt2; }
}
namespace zmile {
template <class T> struct is_modint : public std::false_type { }; template <class T, T M> struct is_modint<ModInt<T, M>> : public std::true_type { }; template <class T> constexpr bool is_modint_v = is_modint<T>::value;
template <class T> struct get_modint_mod; template <class T, T M> struct get_modint_mod<ModInt<T, M>> { static const T value = M; }; template <class T> constexpr auto get_modint_mod_v = get_modint_mod<T>::value;
template<class ValueT = ModInt<>> requires( requires(ValueT x) { x + x; x * x; x / ValueT(10); ValueT(1) / x; x = 1; } ) class FFT { size_t n; std::vector<size_t> rev; using poly_t = std::vector<ValueT>; const double PI = std::acos(-1);
public: FFT(size_t n = 0) { if (n) set_size(n); }
enum class TransformDirection : int8_t { DFT = 1, IDFT = -1 };
void set_size(size_t size) { for (size_t L = 1; (n = L) <= size; L <<= 1); rev.assign(n, 0); for (size_t i = 1; i < n; i++) rev[i] = rev[i >> 1] >> 1 | (n >> 1) * (i & 1); }
inline poly_t transform(const poly_t& poly, TransformDirection dir) { poly_t result; transform(poly, result, dir); return result; }
inline void transform(const poly_t& poly, poly_t& result, TransformDirection dir) { if (poly.empty()) return result.clear();
if (n < poly.size()) set_size(poly.size());
if (&result != &poly) result = poly; result.resize(n); if constexpr (is_modint_v<ValueT>) { if (get_max_valid_length(get_modint_mod_v<ValueT>-1) < n) throw std::logic_error("FATAL ERR: the mod prime is not strong enough to hold the NTT result!"); }
for (size_t i = 0; i < n; i++) { if (i < rev[i]) std::swap(result[i], result[rev[i]]); }
for (size_t h = 2; h <= n; h <<= 1) {
ValueT wn, w; if constexpr (is_modint_v<ValueT>) { wn = qpow(ValueT(find_primitive_root<get_modint_mod_v<ValueT>>()), (get_modint_mod_v<ValueT> - 1) / h); } else if constexpr (std::is_same_v<ValueT, std::complex<double>>) { wn = { cos(2*PI/h), sin(2*PI/h) }; } else { []<bool flag = false>(){ static_assert(flag, "FATAL ERR: FFT only support std::complex<double> and ModInt."); }(); }
if (dir == TransformDirection::IDFT) wn = ValueT(1) / wn;
for (size_t j = 0; j < n; j += h) { w = 1; for (size_t k = j; k < j + (h >> 1); k++, w *= wn) { ValueT x = result[k], y = result[k + (h >> 1)] * w; result[k] = x + y, result[k + (h >> 1)] = x - y; } } }
if (dir == TransformDirection::IDFT) { for (size_t i = 0; i < n; i++) result[i] /= n; } } };
}
const int MOD = 1004535809; using num_t = std::complex<double>; using dir_t = zmile::FFT<num_t>::TransformDirection;
int main() { size_t n, m; num_t val; std::cin >> n >> m; zmile::FFT<num_t> transformer{ n + m + 1 }; std::vector<num_t> poly1, poly2;
for (int i = 0; i <= n; i++) { std::cin >> val; poly1.push_back(val); } for (int i = 0; i <= m; i++) { std::cin >> val; poly2.push_back(val); }
transformer.transform(poly1, poly1, dir_t::DFT); transformer.transform(poly2, poly2, dir_t::DFT);
for (int i = 0; i < poly1.size(); i++) { poly1[i] *= poly2[i]; }
transformer.transform(poly1, poly1, dir_t::IDFT); for (int i = 0; i <= n + m; i++) { std::cout << int(poly1[i].real() + 0.5) << ' '; }
return 0; }
|