Submit Info #57187

Problem Lang User Status Time Memory
Multipoint Evaluation cpp tokusakurai AC 1199 ms 27.20 MiB

ケース詳細
Name Status Time Memory
example_00 AC 1 ms 0.61 MiB
example_01 AC 1 ms 0.71 MiB
max_random_00 AC 1199 ms 27.20 MiB
max_random_01 AC 1196 ms 27.20 MiB
random_00 AC 217 ms 11.36 MiB
random_01 AC 200 ms 8.62 MiB
random_02 AC 1030 ms 23.72 MiB

//ラグランジュ補間 //計算量 O(N+log(M)) //概要 //N次多項式f(x)についてf(0),f(1),...,f(N)の値が与えられたときに、与えられた1点xについてf(x)を求める。 //ラグランジュ補間をすると、 //f(x) = Σy_i*Π{(x-j)/(i-j)}(j≠i) //であるから、階乗の逆数と(x-i)の累積積を左右から求めることで高速化する。 //verified with //https://atcoder.jp/contests/arc033/tasks/arc033_4 #include <bits/stdc++.h> using namespace std; struct io_setup{ io_setup(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; const int MOD = 998244353; template<int mod> struct Mod_Int{ int x; Mod_Int() : x(0) {} Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} static int get_mod() {return mod;} Mod_Int &operator += (const Mod_Int &p){ if((x += p.x) >= mod) x -= mod; return *this; } Mod_Int &operator -= (const Mod_Int &p){ if((x += mod - p.x) >= mod) x -= mod; return *this; } Mod_Int &operator *= (const Mod_Int &p){ x = (int) (1LL * x * p.x % mod); return *this; } Mod_Int &operator /= (const Mod_Int &p){ *this *= p.inverse(); return *this; } Mod_Int &operator ++ () {return *this += Mod_Int(1);} Mod_Int operator ++ (int){ Mod_Int tmp = *this; ++*this; return tmp; } Mod_Int &operator -- () {return *this -= Mod_Int(1);} Mod_Int operator -- (int){ Mod_Int tmp = *this; --*this; return tmp; } Mod_Int operator - () const {return Mod_Int(-x);} Mod_Int operator + (const Mod_Int &p) const {return Mod_Int(*this) += p;} Mod_Int operator - (const Mod_Int &p) const {return Mod_Int(*this) -= p;} Mod_Int operator * (const Mod_Int &p) const {return Mod_Int(*this) *= p;} Mod_Int operator / (const Mod_Int &p) const {return Mod_Int(*this) /= p;} bool operator == (const Mod_Int &p) const {return x == p.x;} bool operator != (const Mod_Int &p) const {return x != p.x;} Mod_Int inverse() const { assert(*this != Mod_Int(0)); return pow(mod-2); } Mod_Int pow(long long k) const{ Mod_Int now = *this, ret = 1; for(; k > 0; k >>= 1, now *= now){ if(k&1) ret *= now; } return ret; } friend ostream &operator << (ostream &os, const Mod_Int &p){ return os << p.x; } friend istream &operator >> (istream &is, Mod_Int &p){ long long a; is >> a; p = Mod_Int<mod>(a); return is; } }; using mint = Mod_Int<MOD>; template<typename T> struct Number_Theorem_Transform{ static int max_base; static T root; static vector<T> r, ir; Number_Theorem_Transform() {} static void init(){ if(!empty(r)) return; int mod = T::get_mod(); int tmp = mod-1; root = 2; while(root.pow(tmp>>1) == 1) root++; max_base = 0; while(tmp%2 == 0) tmp >>= 1, max_base++; r.resize(max_base), ir.resize(max_base); for(int i = 0; i < max_base; i++){ r[i] = -root.pow((mod-1)>>(i+2)); //r[i]:=1の2^(i+2)乗根 ir[i] = r[i].inverse(); //ir[i]:=1/r[i] } } static void ntt(vector<T> &a){ init(); int n = a.size(); assert((n&(n-1)) == 0); assert(n <= (1<<max_base)); for(int k = n; k >>= 1;){ T w = 1; for(int s = 0, t = 0; s < n; s += 2*k){ for(int i = s, j = s+k; i < s+k; i++, j++){ T x = a[i], y = w*a[j]; a[i] = x+y, a[j] = x-y; } w *= r[__builtin_ctz(++t)]; } } } static void intt(vector<T> &a){ init(); int n = a.size(); assert((n&(n-1)) == 0); assert(n <= (1<<max_base)); for(int k = 1; k < n; k <<= 1){ T w = 1; for(int s = 0, t = 0; s < n; s += 2*k){ for(int i = s, j = s+k; i < s+k; i++, j++){ T x = a[i], y = a[j]; a[i] = x+y, a[j] = w*(x-y); } w *= ir[__builtin_ctz(++t)]; } } T inv = T(n).inverse(); for(auto &e: a) e *= inv; } static vector<T> convolve(vector<T> a, vector<T> b){ int k = (int)a.size()+(int)b.size()-1, n = 1; while(n < k) n <<= 1; a.resize(n), b.resize(n); ntt(a), ntt(b); for(int i = 0; i < n; i++) a[i] *= b[i]; intt(a), a.resize(k); return a; } }; template<typename T> int Number_Theorem_Transform<T>::max_base = 0; template<typename T> T Number_Theorem_Transform<T>::root = T(); template<typename T> vector<T> Number_Theorem_Transform<T>::r = vector<T>(); template<typename T> vector<T> Number_Theorem_Transform<T>::ir = vector<T>(); using NTT = Number_Theorem_Transform<mint>; template<typename T> struct Formal_Power_Series : vector<T>{ using NTT_ = Number_Theorem_Transform<T>; using vector<T>::vector; Formal_Power_Series(const vector<T> &v) : vector<T>(v) {} Formal_Power_Series pre(int n) const{ return Formal_Power_Series(begin(*this), begin(*this)+min((int)this->size(), n)); } Formal_Power_Series rev(int deg = -1) const{ Formal_Power_Series ret = *this; if(deg != -1) ret.resize(deg, T(0)); reverse(begin(ret), end(ret)); return ret; } void normalize(){ while(!this->empty() && this->back() == 0) this->pop_back(); } Formal_Power_Series operator - () const{ Formal_Power_Series ret = *this; for(int i = 0; i < (int)ret.size(); i++) ret[i] = -ret[i]; return ret; } Formal_Power_Series &operator += (const T &x){ if(this->empty()) this->resize(1); (*this)[0] += x; return *this; } Formal_Power_Series &operator += (const Formal_Power_Series &v){ if(v.size() > this->size()) this->resize(v.size()); for(int i = 0; i < (int)v.size(); i++) (*this)[i] += v[i]; this->normalize(); return *this; } Formal_Power_Series &operator -= (const T &x){ if(this->empty()) this->resize(1); *this[0] -= x; return *this; } Formal_Power_Series &operator -= (const Formal_Power_Series &v){ if(v.size() > this->size()) this->resize(v.size()); for(int i = 0; i < (int)v.size(); i++) (*this)[i] -= v[i]; this->normalize(); return *this; } Formal_Power_Series &operator *= (const T &x){ for(int i = 0; i < (int)this->size(); i++) (*this)[i] *= x; return *this; } Formal_Power_Series &operator *= (const Formal_Power_Series &v){ if(this->empty() || empty(v)){ this->clear(); return *this; } return *this = NTT_::convolve(*this, v); } Formal_Power_Series &operator /= (const T &x){ assert(x != 0); T inv = x.inverse(); for(int i = 0; i < (int)this->size(); i++) (*this)[i] *= inv; return *this; } Formal_Power_Series &operator /= (const Formal_Power_Series &v){ if(v.size() > this->size()){ this->clear(); return *this; } int n = this->size()-(int)v.size()+1; return *this = (rev().pre(n)*v.rev().inv(n)).pre(n).rev(n); } Formal_Power_Series &operator %= (const Formal_Power_Series &v){ return *this -= (*this/v)*v; } Formal_Power_Series &operator <<= (int x){ Formal_Power_Series ret(x, 0); ret.insert(end(ret), begin(*this), end(*this)); return *this = ret; } Formal_Power_Series &operator >>= (int x){ Formal_Power_Series ret; ret.insert(end(ret), begin(*this)+x, end(*this)); return *this = ret; } Formal_Power_Series operator + (const T &x) const {return Formal_Power_Series(*this) += x;} Formal_Power_Series operator + (const Formal_Power_Series &v) const {return Formal_Power_Series(*this) += v;} Formal_Power_Series operator - (const T &x) const {return Formal_Power_Series(*this) -= x;} Formal_Power_Series operator - (const Formal_Power_Series &v) const {return Formal_Power_Series(*this) -= v;} Formal_Power_Series operator * (const T &x) const {return Formal_Power_Series(*this) *= x;} Formal_Power_Series operator * (const Formal_Power_Series &v) const {return Formal_Power_Series(*this) *= v;} Formal_Power_Series operator / (const T &x) const {return Formal_Power_Series(*this) /= x;} Formal_Power_Series operator / (const Formal_Power_Series &v) const {return Formal_Power_Series(*this) /= v;} Formal_Power_Series operator % (const Formal_Power_Series &v) const {return Formal_Power_Series(*this) %= v;} Formal_Power_Series operator << (int x) const {return Formal_Power_Series(*this) <<= x;} Formal_Power_Series operator >> (int x) const {return Formal_Power_Series(*this) >>= x;} T val(const T &x) const{ T ret = 0; for(int i = (int)this->size()-1; i >= 0; i--) ret *= x, ret += (*this)[i]; return ret; } Formal_Power_Series diff() const{ // df/dx int n = this->size(); Formal_Power_Series ret(n-1); for(int i = 1; i < n; i++) ret[i-1] = (*this)[i]*i; return ret; } Formal_Power_Series integral() const{ // ∫fdx int n = this->size(); Formal_Power_Series ret(n+1); for(int i = 0; i < n; i++) ret[i+1] = (*this)[i]/(i+1); return ret; } Formal_Power_Series inv(int deg) const{ // 1/f (f[0] != 0) assert((*this)[0] != T(0)); Formal_Power_Series ret(1, (*this)[0].inverse()); for(int i = 1; i < deg; i <<= 1){ Formal_Power_Series f = pre(2*i), g = ret; f.resize(2*i), g.resize(2*i); NTT_::ntt(f), NTT_::ntt(g); Formal_Power_Series h(2*i); for(int j = 0; j < 2*i; j++) h[j] = f[j]*g[j]; NTT_::intt(h); for(int j = 0; j < i; j++) h[j] = 0; NTT_::ntt(h); for(int j = 0; j < 2*i; j++) h[j] *= g[j]; NTT_::intt(h); for(int j = 0; j < i; j++) h[j] = 0; ret -= h; //ret = (ret+ret-ret*ret*pre(i<<1)).pre(i<<1); } ret.resize(deg); return ret; } Formal_Power_Series inv() const {return inv(this->size());} Formal_Power_Series log(int deg) const{ // log(f) (f[0] = 1) assert((*this)[0] == 1); Formal_Power_Series ret = (diff()*inv(deg)).pre(deg-1).integral(); ret.resize(deg); return ret; } Formal_Power_Series log() const {return log(this->size());} Formal_Power_Series exp(int deg) const{ // exp(f) (f[0] = 0) assert((*this)[0] == 0); Formal_Power_Series ret(1, 1); for(int i = 1; i < deg; i <<= 1){ ret = (ret*(pre(i<<1)+1-ret.log(i<<1))).pre(i<<1); } ret.resize(deg); return ret; } Formal_Power_Series exp() const {return exp(this->size());} Formal_Power_Series pow(long long k, int deg) const{ // f^k int n = this->size(); for(int i = 0; i < n; i++){ if((*this)[i] == 0) continue; T rev = (*this)[i].inverse(); Formal_Power_Series C(*this*rev), D(n-i, 0); for(int j = i; j < n; j++) D[j-i] = C[j]; D = (D.log()*k).exp()*((*this)[i].pow(k)); Formal_Power_Series E(deg, 0); if(i > 0 && k > deg/i) return E; long long S = i*k; for(int j = 0; j+S < deg && j < D.size(); j++) E[j+S] = D[j]; E.resize(deg); return E; } return Formal_Power_Series(deg, 0); } Formal_Power_Series pow(long long k) const {return pow(k, this->size());} }; using fps = Formal_Power_Series<mint>; template<typename T> vector<Formal_Power_Series<T>> subproduct_tree(const vector<T> &xs){ int n = xs.size(); int k = 1; while(k < n) k <<= 1; vector<Formal_Power_Series<T>> g(2*k, {1}); for(int i = 0; i < n; i++) g[k+i] = {-xs[i], 1}; for(int i = k-1; i > 0; i--) g[i] = g[2*i]*g[2*i+1]; return g; } template<typename T> vector<T> multipoint_evaluation(const Formal_Power_Series<T> &f, const vector<T> &xs){ vector<Formal_Power_Series<T>> g = subproduct_tree(xs); int n = xs.size(), k = g.size()/2; g[1] = f%g[1]; for(int i = 2; i < k+n; i++) g[i] = g[i/2]%g[i]; vector<T> ys(n); for(int i = 0; i < n; i++) ys[i] = g[k+i][0]; return ys; } int main(){ int N, M; cin >> N >> M; fps f(N, 1); vector<mint> xs(M, 1); for(int i = 0; i < N; i++) cin >> f[i]; for(int i = 0; i < M; i++) cin >> xs[i]; vector<mint> ys = multipoint_evaluation(f, xs); for(int i = 0; i < M; i++) cout << ys[i] << (i == M-1? '\n' : ' '); }