結果

問題 No.1145 Sums of Powers
ユーザー eQeeQe
提出日時 2024-11-11 03:36:05
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 10,139 bytes
コンパイル時間 6,971 ms
コンパイル使用メモリ 338,436 KB
実行使用メモリ 31,276 KB
最終ジャッジ日時 2024-11-11 03:36:17
合計ジャッジ時間 9,607 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 5 ms
5,248 KB
testcase_03 AC 492 ms
29,864 KB
testcase_04 AC 473 ms
30,120 KB
testcase_05 AC 442 ms
31,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
namespace my{
using namespace std;
using ml=atcoder::modint998244353;
auto&operator>>(istream&i,ml&x){int t;i>>t;x=t;return i;}
auto&operator<<(ostream&o,const ml&x){return o<<(int)x.val();}
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define VRD(T,n,...) vec<T>__VA_ARGS__;setsize({n},__VA_ARGS__);lin(__VA_ARGS__)
#define FO(n) for(ll ij=n;ij--;)
#define FOR(i,...) for(auto[i,i##stop,i##step]=range(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fo(i,...) FO##__VA_OPT__(R)(i __VA_OPT__(,__VA_ARGS__))
#define fe(a,i,...) for(auto&&__VA_OPT__([)i __VA_OPT__(,__VA_ARGS__]):a)
#define single_testcase void solve();}int main(){my::io();my::solve();}namespace my{
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
template<class...A>auto range(bool s,A...a){array<ll,3>r{0,0,1};ll I=0;((r[I++]=a),...);if(!s&&I==1)swap(r[0],r[1]);r[0]-=s;return r;}
constexpr char nl=10;
constexpr char sp=32;
auto min(const auto&...a){return min(initializer_list<common_type_t<decltype(a)...>>{a...});}

template<class A,class B>struct pair{
  A a;B b;
  pair()=default;
  pair(A a,B b):a(a),b(b){}
  pair(const std::pair<A,B>&p):a(p.first),b(p.second){}
  auto operator<=>(const pair&)const=default;
  friend ostream&operator<<(ostream&o,const pair&p){return o<<p.a<<sp<<p.b;}
};

auto pop_front(auto&a){assert(a.size());auto r=*a.begin();a.pop_front();return r;}

template<class T,class U>ostream&operator<<(ostream&o,const std::pair<T,U>&p){return o<<p.first<<sp<<p.second;}
template<class T,size_t n>ostream&operator<<(ostream&o,const array<T,n>&a){fo(i,n)o<<a[i]<<string(i!=n-1,sp);return o;}

template<class T,class F>struct priority_queue:std::priority_queue<T,vector<T>,F>{
  array<T,1>one;
  priority_queue(const initializer_list<T>&a={}){fe(a,e)this->emplace(e);}
  priority_queue(const vector<T>&a){fe(a,e)this->emplace(e);}
  T sum()const{auto q=*this;T r{};while(q.size())r+=q.front(),q.pop_front();return r;}
  auto begin(){*one.begin()=this->top();return one.begin();}
  T front(){return this->top();}
  void pop_front(){this->pop();}
  friend ostream&operator<<(ostream&o,priority_queue q){while(q.size())o<<my::pop_front(q)<<string(q.size()>0,sp);return o;}
};
template<class T>using min_heap=priority_queue<T,greater<T>>;

template<class V>concept vectorial=is_base_of_v<vector<typename V::value_type>,V>;
template<class T>struct vec_attr{using core_type=T;static constexpr int d=0;};
template<vectorial V>struct vec_attr<V>{using core_type=typename vec_attr<typename V::value_type>::core_type;static constexpr int d=vec_attr<typename V::value_type>::d+1;};
template<class T>using core_t=vec_attr<T>::core_type;
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>ostream&operator<<(ostream&o,const vector<V>&v){fe(v,e)o<<e<<string(&e!=&v.back(),vectorial<V>?nl:sp);return o;}

template<class V>struct vec:vector<V>{
  using vector<V>::vector;
  vec(const vector<V>&v){vector<V>::operator=(v);}

  vec&operator^=(const vec&u){this->insert(this->end(),u.begin(),u.end());return*this;}
  vec operator^(const vec&u)const{return vec{*this}^=u;}
  vec&operator++(){fe(*this,e)++e;return*this;}
  vec&operator--(){fe(*this,e)--e;return*this;}

  auto scan(const auto&f)const{pair<core_t<V>,bool>r{};fe(*this,e)if constexpr(!vectorial<V>)r.b?f(r.a,e),r:r={e,1};else if(auto s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;return r;}
  auto sum()const{return scan([](auto&a,const auto&b){a+=b;}).a;}
  auto min()const{return scan([](auto&a,const auto&b){a>b?a=b:0;;}).a;}

  void pop_front(){this->erase(this->begin());}
};
template<class T=ll,size_t n,size_t i=0>auto make_vec(const ll(&s)[n],T x={}){if constexpr(n==i+1)return vec<T>(s[i],x);else{auto X=make_vec<T,n,i+1>(s,x);return vec<decltype(X)>(s[i],X);}}
template<ll n,class...A>void setsize(const ll(&l)[n],A&...a){((a= make_vec(l,core_t<A>())),...);}

void lin(auto&...a){(cin>>...>>a);}
template<char c=sp>void pp(const auto&...a){ll n=sizeof...(a);((cout<<a<<string(--n>0,c)),...);cout<<nl;}

auto mod(auto a,auto m){return(a%=m)<0?a+m:a;}

namespace fft{
using real=double;
struct complex{
  real x,y;
  complex()=default;
  complex(real x,real y):x(x),y(y){}
  inline complex operator+(const complex &c)const{return complex(x+c.x,y+c.y);}
  inline complex operator-(const complex &c)const{return complex(x-c.x,y-c.y);}
  inline complex operator*(const complex &c)const{return complex(x*c.x-y*c.y,x*c.y+y*c.x);}
  inline complex conj()const{return complex(x,-y);}
};

const real PI=acosl(-1);
ll base=1;
vector<complex>rts={{0,0},{1,0}};
vector<int>fft_rev={0,1};

void ensure_base(int nbase){
  if(nbase<=base)return;
  fft_rev.resize(1<<nbase);
  rts.resize(1<<nbase);
  fo(i,1<<nbase)fft_rev[i]=(fft_rev[i>>1]>>1)+((i&1)<<(nbase-1));
  while(base<nbase){
    real angle=PI*2.0/(1<<(base+1));
    fo(i,1<<(base-1),1<<base){
      rts[i<<1]=rts[i];
      real angle_i=angle*(2*i+1-(1<<base));
      rts[(i<<1)+1]=complex(std::cos(angle_i),std::sin(angle_i));
    }
    ++base;
  }
}

void fast_fourier_transform(vector<complex>&a,int n){
  assert((n&(n-1))==0);
  int zeros=__builtin_ctz(n);
  ensure_base(zeros);
  int shift=base-zeros;
  fo(i,n)if(i<(fft_rev[i]>>shift))swap(a[i],a[fft_rev[i]>>shift]);

  for(int k=1;k<n;k<<=1){
    for(int i=0;i<n;i+=2*k){
      for(int j=0;j<k;j++){
        complex z=a[i+j+k]*rts[j+k];
        a[i+j+k]=a[i+j]-z;
        a[i+j]=a[i+j]+z;
      }
    }
  }
}
}

template<class T>struct arbitrary_mod_convolution{
  using real=fft::real;
  using complex=fft::complex;
  arbitrary_mod_convolution(){}

  std::vector<T>multiply(const std::vector<T>&a,const std::vector<T>&b,int need=-1){
    if(need==-1)need=a.size()+b.size()-1;
    int nbase=0;
    while((1<<nbase)<need)nbase++;
    fft::ensure_base(nbase);
    int sz=1<<nbase;
    std::vector<complex>fa(sz);
    fo(i,a.size())fa[i]=complex(a[i].val()&((1<<15)-1),a[i].val()>>15);

    fft::fast_fourier_transform(fa,sz);
    std::vector<complex>fb(sz);
    if(a==b){
      fb=fa;
    }else{
      fo(i,b.size())fb[i]=complex(b[i].val()&((1<<15)-1),b[i].val()>>15);
      fft::fast_fourier_transform(fb,sz);
    }
    real ratio=0.25/sz;
    complex r2(0,-1),r3(ratio,0),r4(0,-ratio),r5(0,1);
    for(int i=0;i<=(sz>>1);i++){
      int j=(sz-i)&(sz-1);
      complex a1=(fa[i]+fa[j].conj());
      complex a2=(fa[i]-fa[j].conj())*r2;
      complex b1=(fb[i]+fb[j].conj())*r3;
      complex b2=(fb[i]-fb[j].conj())*r4;
      if(i!=j){
        complex c1=(fa[j]+fa[i].conj());
        complex c2=(fa[j]-fa[i].conj())*r2;
        complex d1=(fb[j]+fb[i].conj())*r3;
        complex d2=(fb[j]-fb[i].conj())*r4;
        fa[i]=c1*d1+c2*d2*r5;
        fb[i]=c1*d2+c2*d1;
      }
      fa[j]=a1*b1+a2*b2*r5;
      fb[j]=a1*b2+a2*b1;
    }
    fft::fast_fourier_transform(fa,sz);
    fft::fast_fourier_transform(fb,sz);
    std::vector<T>ret(need);
    fo(i,need){
      int64_t aa=llround(fa[i].x);
      int64_t bb=llround(fb[i].x);
      int64_t cc=llround(fa[i].y);
      aa=T(aa).val(),bb=T(bb).val(),cc=T(cc).val();
      ret[i]=aa+(bb<<15)+(cc<<30);
    }
    return ret;
  }
};

template<class T>struct formal_power_series:vec<T>{
  using vec<T>::vec;
  using fps=formal_power_series;

  static inline arbitrary_mod_convolution<T>fft;
  static fps mul(const fps&a,const fps&b){
    if constexpr(T::mod()==998244353)return convolution(a,b);
    else return fft.multiply(a,b);
  }

  auto operator<=>(const fps&f)const{return this->size()<=>f.size();}

  fps pre(ll deg)const{fps r(this->begin(),this->begin()+min((ll)this->size(),deg));r.resize(deg);return r;}

  fps&operator+=(const fps&f){if(f.size()>this->size())this->resize(f.size());fo(i,f.size())(*this)[i]+=f[i];return*this;}
  fps&operator-=(const fps&f){if(f.size()>this->size())this->resize(f.size());fo(i,f.size())(*this)[i]-=f[i];return*this;}
  fps&operator*=(const fps&f){return*this=(this->size()&&f.size()?mul(*this,f):fps{});}

  fps&operator>>=(ll sz){if((ll)this->size()<=sz)return*this=fps{};this->erase(this->begin(),this->begin()+sz);return*this;}
  fps&operator<<=(ll sz){this->insert(this->begin(),sz,T{});return*this;}

  fps&operator/=(const fps&f){
    ll I1,I2;
    for(I1=0;I1<this->size()&&(*this)[I1]==0;++I1);
    for(I2=0;I2<f.size()&&f[I2]==0;++I2);
    assert(I1>=I2);
    return*this=((*this>>I2)*(f>>I2).inv(this->size())).pre(this->size());
  }

  fps operator+(const fps&f)const{return fps{*this}+=f;}
  fps operator-(const fps&f)const{return fps{*this}-=f;}
  fps operator*(const fps&f)const{return fps{*this}*=f;}
  fps operator/(const fps&f)const{return fps{*this}/=f;}
  fps operator-()const{auto r=*this;fe(r,x)x=-x;return r;}
  fps operator>>(ll sz)const{return fps{*this}>>=sz;}
  fps operator<<(ll sz)const{return fps{*this}<<=sz;}

  fps&operator+=(const T&c){if(!this->size())this->resize(1);(*this)[0]+=c;return*this;}
  fps&operator-=(const T&c){if(!this->size())this->resize(1);(*this)[0]-=c;return*this;}
  fps&operator*=(const T&c){fo(i,this->size())(*this)[i]*=c;return*this;}
  fps operator+(const T&c)const{return fps{*this}+=c;}
  fps operator-(const T&c)const{return fps{*this}-=c;}
  fps operator*(const T&c)const{return fps{*this}*=c;}

  T operator()(T x)const{T r=0,xi=1;fe(*this,ai)r+=ai*xi,xi*=x;return r;}

  fps inv(ll deg=-1)const{
    assert((*this)[0]!=T{});
    if(deg==-1)deg=this->size();
    fps r{T{1}/(*this)[0]};
    for(ll i=1;i<deg;i<<=1)r=(r*2-this->pre(i<<1)*(r*r)).pre(i<<1);
    return r.pre(deg);
  }

  static fps sum(const vec<fps>&F,const vec<fps>&G,ll deg=-1){
    if(F.size()==0)return fps{0};
    min_heap<pair<fps,fps>>q;
    fo(i,F.size())q.emplace(G[i],F[i]);
    while(q.size()>1){
      auto[g1,f1]=q.top();q.pop();
      auto[g2,f2]=q.top();q.pop();
      q.emplace(g1*g2,f1*g2+g1*f2);
    }
    auto[g,f]=q.top();
    if(deg!=-1)f.resize(deg);
    return f/g;
  }
};
template<class T>using fps=formal_power_series<T>;

single_testcase
void solve(){
  LL(N,M);
  VRD(ml,N,a);
  vec<fps<ml>>f(N,fps<ml>{1}),g(N,fps<ml>(2));
  fo(i,N)g[i]={1,-a[i]};

  auto s=fps<ml>::sum(f,g,M+1);
  s.pop_front();
  pp(s);
}}
0