This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/multipoint_evaluation"
#include "../../fps/multipoint_evaluation.hpp"
#include "../../convolution/ntt.hpp"
#include "../../fps/fps.hpp"
#include "../../utility/modint.hpp"
#include "../../template/template.hpp"
namespace lib {
using mint = modint998244353;
using FPS = FormalPowerSeries<mint>;
void main_() {
int n, m;
std::cin >> n >> m;
FPS f(n);
rep(i,0,n) std::cin >> f[i].val();
std::vector<mint> p(m);
rep(i,0,m) std::cin >> p[i].val();
auto fp = multipoint_evaluation<mint>(f, p);
rep(i,0,m) {
std::cout << fp[i].val() << " \n"[i == m-1];
}
}
} // namespace ebi
int main() {
int t = 1;
// std::cin >> t;
while (t--) {
lib::main_();
}
return 0;
}
#line 1 "test/polynomial/Multipoint_Evaluation.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/multipoint_evaluation"
#line 2 "fps/multipoint_evaluation.hpp"
#line 2 "fps/fps.hpp"
#line 2 "convolution/ntt4.hpp"
#line 2 "utility/modint.hpp"
#line 2 "template/template.hpp"
#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <typename T> bool chmin(T &a, const T &b) {
if (a <= b) return false;
a = b;
return true;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a >= b) return false;
a = b;
return true;
}
namespace lib {
using namespace std;
} // namespace lib
// using namespace lib;
#line 4 "utility/modint.hpp"
namespace lib {
template <ll m> struct modint {
using mint = modint;
ll a;
modint(ll x = 0) : a((x % m + m) % m) {}
static constexpr ll mod() {
return m;
}
ll val() const {
return a;
}
ll& val() {
return a;
}
mint pow(ll n) const {
mint res = 1;
mint x = a;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
mint inv() const {
return pow(m - 2);
}
mint& operator+=(const mint rhs) {
a += rhs.a;
if (a >= m) a -= m;
return *this;
}
mint& operator-=(const mint rhs) {
if (a < rhs.a) a += m;
a -= rhs.a;
return *this;
}
mint& operator*=(const mint rhs) {
a = a * rhs.a % m;
return *this;
}
mint& operator/=(mint rhs) {
*this *= rhs.inv();
return *this;
}
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 modint &lhs, const modint &rhs) {
return lhs.a == rhs.a;
}
friend bool operator!=(const modint &lhs, const modint &rhs) {
return !(lhs == rhs);
}
mint operator+() const {
return *this;
}
mint operator-() const {
return mint() - *this;
}
};
using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1'000'000'007>;
} // namespace lib
#line 4 "convolution/ntt4.hpp"
namespace lib {
// only for modint998244353
template<typename mint>
struct NTT {
using uint = unsigned int;
static constexpr uint mod = mint::mod();
static constexpr ull mod2 = (ull)mod * mod;
static constexpr uint pr = 3; // for modint998244353
static constexpr int level = 23; // for modint998244353
array<mint,level+1> wp, wm;
void set_ws(){
mint r = mint(pr).pow((mod-1) >> level);
wp[level] = r, wm[level] = r.inv();
for (int i = level-1; i >= 0; i--){
wp[i] = wp[i+1] * wp[i+1];
wm[i] = wm[i+1] * wm[i+1];
}
}
NTT () { set_ws(); }
void fft4(vector<mint> &a, int k){
uint im = wm[2].val();
uint n = 1<<k;
uint len = n;
int d = k;
while (len > 1){
if (d == 1){
for (int i = 0; i < (1<<(k-1)); i++){
a[i*2+0] += a[i*2+1];
a[i*2+1] = a[i*2+0] - a[i*2+1] * 2;
}
len >>= 1;
d -= 1;
}
else {
int len4 = len/4;
int nlen = n/len;
ull r1 = 1, r2 = 1, r3 = 1, imr1 = im, imr3 = im;
for (int i = 0; i < len4; i++){
for (int j = 0; j < nlen; j++){
uint a0 = a[len4*0+i + len*j].val();
uint a1 = a[len4*1+i + len*j].val();
uint a2 = a[len4*2+i + len*j].val();
uint a3 = a[len4*3+i + len*j].val();
uint a0p2 = a0 + a2;
uint a1p3 = a1 + a3;
ull b0m2 = (a0 + mod - a2) * r1;
ull b1m3 = (a1 + mod - a3) * imr1;
ull c0m2 = (a0 + mod - a2) * r3;
ull c1m3 = (a1 + mod - a3) * imr3;
a[len4*0+i + len*j] = a0p2 + a1p3;
a[len4*1+i + len*j] = b0m2 + b1m3;
a[len4*2+i + len*j] = (a0p2 + mod*2 - a1p3) * r2;
a[len4*3+i + len*j] = c0m2 + mod2*2 - c1m3;
}
r1 = r1 * wm[d].val() % mod;
r2 = r1 * r1 % mod;
r3 = r1 * r2 % mod;
imr1 = im * r1 % mod;
imr3 = im * r3 % mod;
}
len >>= 2;
d -= 2;
}
}
}
void ifft4(vector<mint> &a, int k){
uint im = wp[2].val();
uint n = 1<<k;
uint len = (k & 1 ? 2 : 4);
int d = (k & 1 ? 1 : 2);
while (len <= n){
if (d == 1){
for (int i = 0; i < (1<<(k-1)); i++){
a[i*2+0] += a[i*2+1];
a[i*2+1] = a[i*2+0] - a[i*2+1] * 2;
}
len <<= 2;
d += 2;
}
else {
int len4 = len/4;
int nlen = n/len;
ull r1 = 1, r2 = 1, r3 = 1, imr1 = im, imr3 = im;
for (int i = 0; i < len4; i++){
for (int j = 0; j < nlen; j++){
ull a0 = a[len4*0+i + len*j].val();
ull a1 = a[len4*1+i + len*j].val() * r1;
ull a2 = a[len4*2+i + len*j].val() * r2;
ull a3 = a[len4*3+i + len*j].val() * r3;
ull b1 = a[len4*1+i + len*j].val() * imr1;
ull b3 = a[len4*3+i + len*j].val() * imr3;
ull a0p2 = a0 + a2;
ull a1p3 = a1 + a3;
ull a0m2 = a0 + mod2 - a2;
ull b1m3 = b1 + mod2 - b3;
a[len4*0+i + len*j] = a0p2 + a1p3;
a[len4*1+i + len*j] = a0m2 + b1m3;
a[len4*2+i + len*j] = a0p2 + mod2*2 - a1p3;
a[len4*3+i + len*j] = a0m2 + mod2*2 - b1m3;
}
r1 = r1 * wp[d].val() % mod;
r2 = r1 * r1 % mod;
r3 = r1 * r2 % mod;
imr1 = im * r1 % mod;
imr3 = im * r3 % mod;
}
len <<= 2;
d += 2;
}
}
}
vector<mint> multiply(const vector<mint> &a, const vector<mint> &b){
if (a.empty() || b.empty()) return {};
int d = a.size() + b.size() - 1;
if (min<int>(a.size(), b.size()) <= 40){
vector<mint> s(d);
rep(i,0,a.size()) rep(j,0,b.size()) s[i+j] += a[i]*b[j];
return s;
}
int k = 2, M = 4;
while (M < d) M <<= 1, ++k;
vector<mint> s(M), t(M);
rep(i,0,a.size()) s[i] = a[i];
rep(i,0,b.size()) t[i] = b[i];
fft4(s,k);
fft4(t,k);
rep(i,0,M) s[i] *= t[i];
ifft4(s, k);
s.resize(d);
mint invm = mint(M).inv();
rep(i,0,d) s[i] *= invm;
return s;
}
};
} // namespace lib
#line 6 "fps/fps.hpp"
namespace lib {
template <class mint> struct FormalPowerSeries : std::vector<mint> {
private:
using FPS = FormalPowerSeries<mint>;
using std::vector<mint>::vector;
using std::vector<mint>::vector::operator=;
NTT<mint> ntt;
public:
FormalPowerSeries(const std::vector<mint> &a) {
*this = a;
}
FPS operator+(const FPS &rhs) const noexcept {
return FPS(*this) += rhs;
}
FPS operator-(const FPS &rhs) const noexcept {
return FPS(*this) -= rhs;
}
FPS operator*(const FPS &rhs) const noexcept {
return FPS(*this) *= rhs;
}
FPS operator/(const FPS &rhs) const noexcept {
return FPS(*this) /= rhs;
}
FPS operator%(const FPS &rhs) const noexcept {
return FPS(*this) %= rhs;
}
FPS operator+(const mint &rhs) const noexcept {
return FPS(*this) += rhs;
}
FPS operator-(const mint &rhs) const noexcept {
return FPS(*this) -= rhs;
}
FPS operator*(const mint &rhs) const noexcept {
return FPS(*this) *= rhs;
}
FPS operator/(const mint &rhs) const noexcept {
return FPS(*this) /= rhs;
}
FPS &operator+=(const FPS &rhs) noexcept {
if (this->size() < rhs.size()) this->resize(rhs.size());
for (int i = 0; i < (int)rhs.size(); ++i) {
(*this)[i] += rhs[i];
}
return *this;
}
FPS &operator-=(const FPS &rhs) noexcept {
if (this->size() < rhs.size()) this->resize(rhs.size());
for (int i = 0; i < (int)rhs.size(); ++i) {
(*this)[i] -= rhs[i];
}
return *this;
}
FPS &operator*=(const FPS &rhs) noexcept {
*this = ntt.multiply(*this, rhs);
return *this;
}
FPS &operator/=(const FPS &rhs) noexcept {
int n = deg() - 1;
int m = rhs.deg() - 1;
if (n < m) {
*this = {};
return *this;
}
*this = (*this).rev() * rhs.rev().inv(n - m + 1);
(*this).resize(n - m + 1);
std::reverse((*this).begin(), (*this).end());
return *this;
}
FPS &operator%=(const FPS &rhs) noexcept {
*this -= *this / rhs * rhs;
shrink();
return *this;
}
FPS &operator+=(const mint &rhs) noexcept {
if (this->empty()) this->resize(1);
(*this)[0] += rhs;
return *this;
}
FPS &operator-=(const mint &rhs) noexcept {
if (this->empty()) this->resize(1);
(*this)[0] -= rhs;
return *this;
}
FPS &operator*=(const mint &rhs) noexcept {
for (int i = 0; i < deg(); ++i) {
(*this)[i] *= rhs;
}
return *this;
}
FPS &operator/=(const mint &rhs) noexcept {
mint inv_rhs = rhs.inv();
for (int i = 0; i < deg(); ++i) {
(*this)[i] *= inv_rhs;
}
return *this;
}
FPS operator>>(int d) const {
if (deg() <= d) return {};
FPS f = *this;
f.erase(f.begin(), f.begin() + d);
return f;
}
FPS operator<<(int d) const {
FPS f = *this;
f.insert(f.begin(), d, 0);
return f;
}
FPS operator-() const {
FPS g(this->size());
for (int i = 0; i < (int)this->size(); i++) g[i] = -(*this)[i];
return g;
}
FPS pre(int sz) const {
return FPS(this->begin(), this->begin() + std::min(deg(), sz));
}
FPS rev() const {
auto f = *this;
std::reverse(f.begin(), f.end());
return f;
}
FPS differential() const {
int n = deg();
FPS g(std::max(0, n - 1));
for (int i = 0; i < n - 1; i++) {
g[i] = (*this)[i + 1] * (i + 1);
}
return g;
}
FPS integral() const {
int n = deg();
FPS g(n + 1);
g[0] = 0;
if (n > 0) g[1] = 1;
auto mod = mint::mod();
for (int i = 2; i <= n; i++) g[i] = (-g[mod % i]) * (mod / i);
for (int i = 0; i < n; i++) g[i + 1] *= (*this)[i];
return g;
}
FPS inv(int d = -1) const {
int n = 1;
if (d < 0) d = deg();
FPS g(n);
g[0] = (*this)[0].inv();
while (n < d) {
n <<= 1;
g = (g * 2 - g * g * this->pre(n)).pre(n);
}
g.resize(d);
return g;
}
FPS log(int d = -1) const {
assert((*this)[0].val() == 1);
if (d < 0) d = deg();
return ((*this).differential() * (*this).inv(d)).pre(d - 1).integral();
}
FPS exp(int d = -1) const {
assert((*this)[0].val() == 0);
int n = 1;
if (d < 0) d = deg();
FPS g(n);
g[0] = 1;
while (n < d) {
n <<= 1;
g = (g * (this->pre(n) - g.log(n) + 1)).pre(n);
}
g.resize(d);
return g;
}
FPS pow(ll k, int d = -1) const {
const int n = deg();
if (d < 0) d = n;
if (k == 0) {
FPS f(d);
if (d > 0) f[0] = 1;
return f;
}
for (int i = 0; i < n; i++) {
if ((*this)[i].val() != 0) {
mint rev = (*this)[i].inv();
FPS f = (((*this * rev) >> i).log(d) * k).exp(d);
f *= (*this)[i].pow(k);
f = (f << (i * k)).pre(d);
if (f.deg() < d) f.resize(d);
return f;
}
if (i + 1 >= (d + k - 1) / k) break;
}
return FPS(d);
}
int deg() const {
return (*this).size();
}
void shrink() {
while ((!this->empty()) && this->back() == 0) this->pop_back();
}
int count_terms() const {
int c = 0;
for (int i = 0; i < deg(); i++) {
if ((*this)[i] != 0) c++;
}
return c;
}
};
} // namespace lib
#line 4 "fps/multipoint_evaluation.hpp"
namespace lib {
template<class mint>
std::vector<mint> multipoint_evaluation(FormalPowerSeries<mint> f, const std::vector<mint> &p) {
using FPS = FormalPowerSeries<mint>;
int m = 1;
while (m < (int)p.size()) m <<= 1;
std::vector<FPS> subproduct_tree(2 * m, {1});
for (int i = 0; i < (int)p.size(); i++) {
subproduct_tree[i + m] = FPS{-p[i], 1};
}
for (int i = m - 1; i >= 1; i--) {
subproduct_tree[i] =
subproduct_tree[2 * i] * subproduct_tree[2 * i + 1];
}
std::vector<FPS> subremainder_tree(2 * m);
subremainder_tree[1] = f % subproduct_tree[1];
for (int i = 2; i < m + (int)p.size(); i++) {
if (subremainder_tree[i / 2].empty()) continue;
subremainder_tree[i] = subremainder_tree[i / 2] % subproduct_tree[i];
}
std::vector<mint> fp(p.size());
for (int i = 0; i < (int)p.size(); i++) {
if (subremainder_tree[i + m].empty())
fp[i] = 0;
else
fp[i] = subremainder_tree[i + m][0];
}
return fp;
}
} // namespace ebi
#line 4 "test/polynomial/Multipoint_Evaluation.test.cpp"
#line 2 "convolution/ntt.hpp"
#line 5 "convolution/ntt.hpp"
namespace lib {
using mint = modint998244353;
struct ntt_info {
static constexpr int rank2 = 23;
const int g = 3;
std::array<std::array<mint, rank2 + 1>, 2> root;
ntt_info() {
root[0][rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
root[1][rank2] = root[0][rank2].inv();
rrep(i, 0, rank2) {
root[0][i] = root[0][i + 1] * root[0][i + 1];
root[1][i] = root[1][i + 1] * root[1][i + 1];
}
}
};
void butterfly(std::vector<mint>& a, bool inverse) {
static ntt_info info;
int n = a.size();
int bit_size = 0;
while ((1 << bit_size) < n) bit_size++;
assert(1 << bit_size == n);
for (int i = 0, j = 1; j < n - 1; j++) {
for (int k = n >> 1; k > (i ^= k); k >>= 1);
if (j < i) {
std::swap(a[i], a[j]);
}
}
rep(bit, 0, bit_size) {
rep(i, 0, n / (1 << (bit + 1))) {
mint zeta1 = 1;
mint zeta2 = info.root[inverse][1];
mint w = info.root[inverse][bit + 1];
rep(j, 0, 1 << bit) {
int idx = i * (1 << (bit + 1)) + j;
int jdx = idx + (1 << bit);
mint p1 = a[idx];
mint p2 = a[jdx];
a[idx] = p1 + zeta1 * p2;
a[jdx] = p1 + zeta2 * p2;
zeta1 *= w;
zeta2 *= w;
}
}
}
if (inverse) {
mint inv_n = mint(n).inv();
rep(i, 0, n) a[i] *= inv_n;
}
}
std::vector<mint> convolution(const std::vector<mint>& f,
const std::vector<mint>& g) {
int n = 1;
while (n < int(f.size() + g.size() - 1)) n <<= 1;
std::vector<mint> a(n), b(n);
std::copy(f.begin(), f.end(), a.begin());
std::copy(g.begin(), g.end(), b.begin());
butterfly(a, false);
butterfly(b, false);
rep(i, 0, n) {
a[i] *= b[i];
}
butterfly(a, true);
a.resize(f.size() + g.size() - 1);
return a;
}
} // namespace lib
#line 9 "test/polynomial/Multipoint_Evaluation.test.cpp"
namespace lib {
using mint = modint998244353;
using FPS = FormalPowerSeries<mint>;
void main_() {
int n, m;
std::cin >> n >> m;
FPS f(n);
rep(i,0,n) std::cin >> f[i].val();
std::vector<mint> p(m);
rep(i,0,m) std::cin >> p[i].val();
auto fp = multipoint_evaluation<mint>(f, p);
rep(i,0,m) {
std::cout << fp[i].val() << " \n"[i == m-1];
}
}
} // namespace ebi
int main() {
int t = 1;
// std::cin >> t;
while (t--) {
lib::main_();
}
return 0;
}