結果
問題 | No.2513 Power Eraser |
ユーザー | Taiki0715 |
提出日時 | 2024-10-20 13:11:50 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,731 ms / 6,000 ms |
コード長 | 22,669 bytes |
コンパイル時間 | 6,705 ms |
コンパイル使用メモリ | 335,276 KB |
実行使用メモリ | 15,484 KB |
最終ジャッジ日時 | 2024-10-20 13:12:55 |
合計ジャッジ時間 | 62,032 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 8 ms
6,824 KB |
testcase_04 | AC | 34 ms
6,816 KB |
testcase_05 | AC | 9 ms
6,820 KB |
testcase_06 | AC | 62 ms
6,816 KB |
testcase_07 | AC | 30 ms
6,816 KB |
testcase_08 | AC | 36 ms
6,820 KB |
testcase_09 | AC | 66 ms
6,820 KB |
testcase_10 | AC | 34 ms
6,816 KB |
testcase_11 | AC | 8 ms
6,816 KB |
testcase_12 | AC | 33 ms
6,816 KB |
testcase_13 | AC | 744 ms
8,948 KB |
testcase_14 | AC | 1,620 ms
15,008 KB |
testcase_15 | AC | 1,634 ms
14,840 KB |
testcase_16 | AC | 775 ms
9,016 KB |
testcase_17 | AC | 825 ms
9,444 KB |
testcase_18 | AC | 1,603 ms
14,912 KB |
testcase_19 | AC | 1,666 ms
15,368 KB |
testcase_20 | AC | 759 ms
9,008 KB |
testcase_21 | AC | 1,622 ms
15,024 KB |
testcase_22 | AC | 1,586 ms
15,032 KB |
testcase_23 | AC | 1,667 ms
15,352 KB |
testcase_24 | AC | 1,695 ms
15,480 KB |
testcase_25 | AC | 1,677 ms
15,420 KB |
testcase_26 | AC | 1,649 ms
15,484 KB |
testcase_27 | AC | 1,692 ms
15,356 KB |
testcase_28 | AC | 1,708 ms
15,356 KB |
testcase_29 | AC | 1,660 ms
15,480 KB |
testcase_30 | AC | 1,677 ms
15,480 KB |
testcase_31 | AC | 1,653 ms
15,356 KB |
testcase_32 | AC | 1,677 ms
15,484 KB |
testcase_33 | AC | 2 ms
6,820 KB |
testcase_34 | AC | 1 ms
6,816 KB |
testcase_35 | AC | 1,689 ms
15,484 KB |
testcase_36 | AC | 1,731 ms
15,388 KB |
7_evil_case_1.txt | AC | 3,761 ms
28,388 KB |
7_evil_case_2.txt | AC | 3,773 ms
28,380 KB |
7_evil_case_3.txt | AC | 3,787 ms
28,384 KB |
7_evil_case_4.txt | AC | 3,830 ms
28,516 KB |
7_evil_case_5.txt | AC | 3,772 ms
28,256 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> #endif using ll=long long; using ull=unsigned long long; using P=pair<ll,ll>; template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>; template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);} template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);} template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;} template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;} template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;} template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;} template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;} template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;} #define reps(i,a,n) for(int i=(a);i<int(n);i++) #define rep(i,n) reps(i,0,n) #define all(x) x.begin(),x.end() #define pcnt(x) __builtin_popcountll(x) #define fin(x) return cout<<x<<'\n',static_cast<void>(0) ll myceil(ll a,ll b){return (a+b-1)/b;} template<typename T,size_t n,size_t id=0> auto vec(const int (&d)[n],const T &init=T()){ if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init)); else return init; } #ifdef LOCAL #include<debug.h> #else #define debug(...) static_cast<void>(0) #define debugg(...) static_cast<void>(0) template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;} #endif void SOLVE(); int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout<<fixed<<setprecision(16); #ifdef LOCAL clock_t start=clock(); #endif int testcase=1; //cin>>testcase; for(int i=0;i<testcase;i++){ SOLVE(); } #ifdef LOCAL cerr<<"time:"; cerr<<(clock()-start)/1000; cerr<<"ms\n"; #endif } constexpr int msb(unsigned int n){return n==0?-1:31-__builtin_clz(n);} constexpr int msb(int n){return n==0?-1:31-__builtin_clz(n);} constexpr int msb(unsigned long long n){return n==0?-1:63-__builtin_clzll(n);} constexpr int msb(long long n){return n==0?-1:63-__builtin_clzll(n);} constexpr int lsb(unsigned int n){return n==0?-1:__builtin_ctz(n);} constexpr int lsb(int n){return n==0?-1:__builtin_ctz(n);} constexpr int lsb(unsigned long long n){return n==0?-1:__builtin_ctzll(n);} constexpr int lsb(long long n){return n==0?-1:__builtin_ctzll(n);} constexpr unsigned int floor_pow2(unsigned int n){return n==0?0:1u<<msb(n);} constexpr int floor_pow2(int n){return n<=0?0:1<<msb(n);} constexpr unsigned long long floor_pow2(unsigned long long n){return n==0?0:1ull<<msb(n);} constexpr long long floor_pow2(long long n){return n<=0?0:1ll<<msb(n);} constexpr unsigned int ceil_pow2(unsigned int n){return n<=1?1:1u<<(msb(n-1)+1);} constexpr int ceil_pow2(int n){return n<=1?1:1<<(msb(n-1)+1);} constexpr unsigned long long ceil_pow2(unsigned long long n){return n<=1?1:1ull<<(msb(n-1)+1);} constexpr long long ceil_pow2(long long n){return n<=1?1:1ll<<(msb(n-1)+1);} constexpr int pow_mod(int a,int n,int mod){ long long res=1; while(n){ if(n&1)res=res*a%mod; a=(long long)a*a%mod; n>>=1; } return res; } constexpr int primitive_root_constexpr(int x){ if(x==167772161)return 3; if(x==469762049)return 3; if(x==754974721)return 11; if(x==880803841)return 26; if(x==998244353)return 3; if(x==2)return 1; int x2=x; int p[20]; int c=0; x--; for(int i=2;i*i<=x;i++){ if(x%i==0){ p[c++]=i; while(x%i==0)x/=i; } } if(x!=1)p[c++]=x; x=x2; for(int g=2;;g++){ bool ok=true; for(int i=0;i<c;i++)if(pow_mod(g,(x-1)/p[i],x)==1){ ok=false; break; } if(ok)return g; } } template<int m> struct ntt_root{ static constexpr int rank2=lsb(m-1); static constexpr int g=primitive_root_constexpr(m); std::array<int,rank2+1>root,invroot; std::array<int,std::max(0,rank2-1)>rate2,invrate2; std::array<int,std::max(0,rank2-2)>rate3,invrate3; constexpr ntt_root(){ root[rank2]=pow_mod(g,m>>rank2,m); invroot[rank2]=pow_mod(root[rank2],m-2,m); for(int i=rank2-1;i>=0;i--){ root[i]=(long long)root[i+1]*root[i+1]%m; invroot[i]=(long long)invroot[i+1]*invroot[i+1]%m; } int prod=1,invprod=1; for(int i=0;i<rank2-1;i++){ rate2[i]=(long long)root[i+2]*prod%m; invrate2[i]=(long long)invroot[i+2]*invprod%m; prod=(long long)prod*invroot[i+2]%m; invprod=(long long)invprod*root[i+2]%m; } prod=invprod=1; for(int i=0;i<rank2-2;i++){ rate3[i]=(long long)root[i+3]*prod%m; invrate3[i]=(long long)invroot[i+3]*invprod%m; prod=(long long)prod*invroot[i+3]%m; invprod=(long long)invprod*root[i+3]%m; } } }; template<typename T> void dft(std::vector<T>&a){ static constexpr ntt_root<T::mod()>r; static constexpr unsigned long long mod2=(unsigned long long)T::mod()*T::mod(); int n=a.size(); int h=lsb(n); int len=0; while(len<h){ if(h-len==1){ T rot=T::raw(1); for(int s=0;s<(1<<len);s++){ int of=s*2; T u=a[of],v=a[of+1]*rot; a[of]=u+v; a[of+1]=u-v; rot*=T::raw(r.rate2[lsb(~(unsigned int)s)]); } len++; } else{ int p=1<<(h-len-2); T rot=T::raw(1),imag=T::raw(r.root[2]); for(int s=0;s<(1<<len);s++){ const unsigned long long rot1=rot.val(),rot2=rot1*rot1%T::mod(),rot3=rot1*rot2%T::mod(); int of=s<<(h-len); for(int i=0;i<p;i++){ const unsigned long long a0=a[i+of].val(),a1=(unsigned long long)a[i+of+p].val()*rot1,a2=(unsigned long long)a[i+of+p*2].val()*rot2,a3=(unsigned long long)a[i+of+p*3].val()*rot3; const unsigned long long m=(unsigned long long)T(a1+mod2-a3).val()*imag.val(); const unsigned long long k=mod2-a2; a[i+of]=a0+a2+a1+a3; a[i+of+p]=a0+a2+(mod2*2-a1-a3); a[i+of+p*2]=a0+k+m; a[i+of+p*3]=a0+k+(mod2-m); } rot*=T::raw(r.rate3[lsb(~(unsigned int)s)]); } len+=2; } } } template<typename T> void idft(std::vector<T>&a){ static constexpr ntt_root<T::mod()>r; static constexpr unsigned long long mod2=(unsigned long long)T::mod()*T::mod(); int n=a.size(); int h=lsb(n); int len=h; while(len){ if(len==1){ int p=1<<(h-1); for(int i=0;i<p;i++){ T u=a[i],v=a[i+p]; a[i]=u+v; a[i+p]=u-v; } len--; } else{ int p=1<<(h-len); T rot=T::raw(1),imag=T::raw(r.invroot[2]); for(int s=0;s<(1<<(len-2));s++){ const unsigned long long rot1=rot.val(),rot2=rot1*rot1%T::mod(),rot3=rot1*rot2%T::mod(); int of=s<<(h-len+2); for(int i=0;i<p;i++){ const unsigned long long a0=a[i+of].val(),a1=a[i+of+p].val(),a2=a[i+of+p*2].val(),a3=a[i+of+p*3].val(); const unsigned long long k=T((T::mod()+a2-a3)*imag.val()).val(); a[i+of]=a0+a1+a2+a3; a[i+of+p]=(a0+T::mod()-a1+k)*rot1; a[i+of+p*2]=(a0+a1+T::mod()*2-a2-a3)*rot2; a[i+of+p*3]=(a0+T::mod()*2-a1-k)*rot3; } rot*=T::raw(r.invrate3[lsb(~(unsigned int)s)]); } len-=2; } } } template<typename T> std::vector<T>ntt_convolution(std::vector<T> a,std::vector<T> b){ int n=a.size(),m=b.size(),s=n+m-1; if(std::min(n,m)<60){ std::vector<T>ret(s,0); if(n<m)for(int i=0;i<m;i++)for(int j=0;j<n;j++)ret[i+j]+=a[j]*b[i]; else for(int i=0;i<n;i++)for(int j=0;j<m;j++)ret[i+j]+=a[i]*b[j]; return ret; } int z=ceil_pow2(s); a.resize(z,0); b.resize(z,0); dft(a),dft(b); std::vector<T>c(z); for(int i=0;i<z;i++)c[i]=a[i]*b[i]; idft(c); T g=T::raw(z).inv(); for(int i=0;i<s;i++)c[i]*=g; return {c.begin(),c.begin()+s}; } template<typename T> void transposed_dft(std::vector<T>&a){ static constexpr ntt_root<T::mod()>r; static constexpr unsigned long long mod2=(unsigned long long)T::mod()*T::mod(); int n=a.size(); int h=lsb(n); int len=h; while(len){ if(len==1){ int p=1<<(h-1); T rot=T::raw(1); for(int i=0;i<p;i++){ T u=a[i],v=a[i+p]; a[i]=u+v; a[i+p]=(u-v)*rot; } len--; } else{ int p=1<<(h-len); T rot=T::raw(1),imag=T::raw(r.root[2]); for(int s=0;s<(1<<(len-2));s++){ const unsigned long long rot1=rot.val(),rot2=rot1*rot1%T::mod(),rot3=rot1*rot2%T::mod(); int of=s<<(h-len+2); for(int i=0;i<p;i++){ const unsigned long long a0=a[i+of].val(),a1=a[i+of+p].val(),a2=a[i+of+p*2].val(),a3=a[i+of+p*3].val(); const unsigned long long k=T((T::mod()+a2-a3)*imag.val()).val(); a[i+of]=a0+a1+a2+a3; a[i+of+p]=(a0+T::mod()-a1+k)*rot1; a[i+of+p*2]=(a0+a1+T::mod()*2-a2-a3)*rot2; a[i+of+p*3]=(a0+T::mod()*2-a1-k)*rot3; } if(s+1!=1<<(len-2))rot*=T::raw(r.rate3[lsb(~(unsigned int)s)]); } len-=2; } } } template<typename T> void transposed_idft(std::vector<T>&a){ static constexpr ntt_root<T::mod()>r; static constexpr unsigned long long mod2=(unsigned long long)T::mod()*T::mod(); int n=a.size(); int h=lsb(n); int len=0; while(len<h){ if(h-len==1){ T rot=T::raw(1); for(int s=0;s<(1<<len);s++){ int of=s*2; T u=a[of],v=a[of+1]*rot; a[of]=u+v; a[of+1]=u-v; if(s+1!=1<<len)rot*=T::raw(r.invrate2[lsb(~(unsigned int)s)]); } len++; } else{ int p=1<<(h-len-2); T rot=T::raw(1),imag=T::raw(r.invroot[2]); for(int s=0;s<(1<<len);s++){ const unsigned long long rot1=rot.val(),rot2=rot1*rot1%T::mod(),rot3=rot1*rot2%T::mod(); int of=s<<(h-len); for(int i=0;i<p;i++){ const unsigned long long a0=a[i+of].val(),a1=(unsigned long long)a[i+of+p].val()*rot1,a2=(unsigned long long)a[i+of+p*2].val()*rot2,a3=(unsigned long long)a[i+of+p*3].val()*rot3; const unsigned long long m=(unsigned long long)T(a1+mod2-a3).val()*imag.val(); const unsigned long long k=mod2-a2; a[i+of]=a0+a2+a1+a3; a[i+of+p]=a0+a2+(mod2*2-a1-a3); a[i+of+p*2]=a0+k+m; a[i+of+p*3]=a0+k+(mod2-m); } if(s+1!=1<<len)rot*=T::raw(r.invrate3[lsb(~(unsigned int)s)]); } len+=2; } } } template<typename T> void ntt_doubling(std::vector<T>&a){ static constexpr ntt_root<T::mod()>r; int n=a.size()/2; std::vector<T>b(a.begin(),a.begin()+n); idft(b); T now=T::raw(n).inv(),zeta=T::raw(r.root[msb(n)+1]); for(int i=0;i<n;i++){ b[i]*=now; now*=zeta; } dft(b); std::copy(b.begin(),b.end(),a.begin()+n); } template<typename T> void transposed_ntt_doubling(std::vector<T>&a){ static constexpr ntt_root<T::mod()>r; int n=a.size()/2; std::vector<T>b(a.begin(),a.begin()+n); a=std::vector<T>(a.begin()+n,a.end()); transposed_dft(a); T now=T::raw(n).inv(),zeta=T::raw(r.root[msb(n)+1]); for(int i=0;i<n;i++)a[i]*=now,now*=zeta; transposed_idft(a); for(int i=0;i<n;i++)a[i]+=b[i]; } #include<type_traits> template<int m> struct mod_int{ private: static constexpr unsigned int umod=static_cast<unsigned int>(m); using uint=unsigned int; using mint=mod_int; uint v; static_assert(m<uint(1)<<31); public: mod_int():v(0){} template<typename T,std::enable_if_t<std::is_signed_v<T>,std::nullptr_t> =nullptr> mod_int(T a){ a%=m; if(a<0)v=a+umod; else v=a; } template<typename T,std::enable_if_t<std::is_unsigned_v<T>,std::nullptr_t> =nullptr> mod_int(T a):v(a%umod){} static constexpr mint raw(int a){ mint ret; ret.v=a; return ret; } inline uint val()const{return this->v;} static constexpr int mod(){return m;} inline mint &operator+=(const mint &b){ this->v+=b.v; if(this->v>=umod)this->v-=umod; return *this; } inline mint &operator-=(const mint &b){ this->v-=b.v; if(this->v>=umod)this->v+=umod; return *this; } inline mint &operator*=(const mint &b){ this->v=((unsigned long long)this->v*b.v)%umod; return *this; } inline mint &operator/=(const mint &b){ *this*=b.inv(); return *this; } inline mint operator+()const{return *this;} inline mint operator-()const{return mint()-*this;} friend inline mint operator+(const mint &a,const mint &b){return mint(a)+=b;} friend inline mint operator-(const mint &a,const mint &b){return mint(a)-=b;} friend inline mint operator*(const mint &a,const mint &b){return mint(a)*=b;} friend inline mint operator/(const mint &a,const mint &b){return mint(a)/=b;} friend inline bool operator==(const mint &a,const mint &b){return a.val()==b.val();} friend inline bool operator!=(const mint &a,const mint &b){return !(a==b);} inline mint operator++(int){ mint ret=*this; *this+=1; return ret; } inline mint operator--(int){ mint ret=*this; *this-=1; return ret; } mint pow(long long n)const{ mint ret=mint::raw(1),a(*this); while(n){ if(n&1)ret*=a; a*=a; n>>=1; } return ret; } inline mint inv()const{ assert(this->v!=0); return pow(m-2); } mint sqrt()const{ if(this->val()<=1)return *this; if constexpr(m%8==1){ mint b=2; while(b.pow((m-1)/2).val()==1)b++; int m2=m-1,e=0; while(m2%2==0)m2>>=1,e++; mint x=this->pow((m2-1)/2); mint y=(*this)*x*x; x*=*this; mint z=b.pow(m2); while(y.val()!=1){ int j=0; mint t=y; while(t.val()!=1)t*=t,j++; z=z.pow(1<<(e-j-1)); x*=z; z*=z; y*=z;e=j; } return x; } else if constexpr(m%8==5){ mint ret=this->pow((m+3)/8); if((ret*ret).val()==this->val())return ret; else return ret*mint::raw(2).pow((m-1)/4); } else{ return this->pow((m+1)/4); } } std::pair<bool,mint> safe_sqrt()const{ if(this->pow((m-1)/2)==1)return std::make_pair(true,this->sqrt()); else return std::make_pair(false,0); } friend std::istream &operator>>(std::istream &is,mint &b){ long long a; is>>a; b=mint(a); return is; } friend std::ostream &operator<<(std::ostream &os,const mint &b){ os<<b.val(); return os; } }; using mint998=mod_int<998244353>; using mint107=mod_int<1000000007>; int sqrt_mod(int n,int mod){ if(n<=1)return n; auto powmod=[&](int a,int b)->int { ll ret=1; while(b){ if(b&1)ret=(ret*a)%mod; a=ll(a)*a%mod; b>>=1; } return ret; }; if(powmod(n,(mod-1)/2)!=1)return -1; if(mod%8==1){ int b=2; while(powmod(b,(mod-1)/2)==1)b++; int m2=mod-1,e=0; while(m2%2==0)m2>>=1,e++; int x=powmod(n,(m2-1)/2); int y=(ll(n)*x%mod)*x%mod; x=ll(x)*n%mod; int z=powmod(b,m2); while(y!=1){ int j=0; int t=y; while(t!=1)t=ll(t)*t%mod,j++; z=powmod(z,1<<(e-j-1)); x=ll(x)*z%mod; z=ll(z)*z%mod; y=ll(y)*z%mod; e=j; } return x; } else if(mod%8==5){ int ret=powmod(n,(mod+3)/8); if(ll(ret)*ret%mod==n)return ret; else{ ret=(ll(ret)*powmod(2,(mod-1)/4))%mod; return ret; } } else return powmod(n,(mod+1)/4); } template<typename T> vector<vector<T>>convolution2d(const vector<vector<T>>&a,const vector<vector<T>>&b){ int x=a.size()+b.size()-1,y=a[0].size()+b[0].size()-1; vector<T>f(a.size()*y),g(b.size()*y); rep(i,a.size())rep(j,a[i].size())f[i*y+j]=a[i][j]; rep(i,b.size())rep(j,b[i].size())g[i*y+j]=b[i][j]; auto c=ntt_convolution(f,g); vector<vector<T>>ret(x,vector<T>(y)); rep(i,x)rep(j,y)ret[i][j]=c[i*y+j]; return ret; } template<typename T> vector<T> fps_inv(const vector<T> &a,int deg=-1){ int n=a.size(); if(deg==-1)deg=n; assert(a[0]!=T(0)); vector<T> ret(deg); ret[0]=a[0].inv(); for(int m=1;m<deg;m<<=1){ vector<T> f(a.begin(),a.begin()+min(n,m*2)); if(f.size()<m*2)f.resize(m*2,0); vector<T> g(ret); f.resize(m*2); dft(f); g.resize(m*2); dft(g); rep(i,m*2)f[i]*=g[i]; idft(f); T inv=T::raw(m*2).inv(); rep(i,m)f[i]=0; reps(i,m,m*2)f[i]*=inv; dft(f); rep(i,m*2)f[i]*=g[i]; idft(f); rep(i,m*2)f[i]*=inv; reps(i,m,min(deg,m*2))ret[i]-=f[i]; } return ret; } template<typename T> vector<T> fps_diff(const vector<T>&a){ int n=a.size(); vector<T>b(max(0,n-1)); reps(i,1,n)b[i-1]=a[i]*i; return b; } template<typename T> vector<T> fps_integral(const vector<T>&a){ int n=a.size(); vector<T>b(n+1); b[0]=0; if(n)b[1]=1; reps(i,2,n+1)b[i]=-b[T::mod()%i]*(T::mod()/i); rep(i,n)b[i+1]*=a[i]; return b; } template<typename T> vector<T> fps_log(const vector<T>&a,int deg=-1){ int n=a.size(); if(deg==-1)deg=n; vector<T>b=fps_integral(ntt_convolution(fps_diff(a),fps_inv(a,deg))); return {b.begin(),b.begin()+deg}; } template<typename T> vector<T> fps_exp(const vector<T>&a,int deg=-1){ assert(a.empty()||a[0]==0); int n=a.size(); if(deg==-1)deg=n; vector<T>b{1}; for(int m=1;m<deg;m*=2){ vector<T>nxt=fps_log(b,m*2); rep(i,m*2)nxt[i]*=-1; rep(i,min(n,m*2))nxt[i]+=a[i]; nxt[0]++; b=ntt_convolution(b,nxt); b.resize(m*2); } return {b.begin(),b.begin()+deg}; } template<typename T> vector<T> fps_pow(vector<T> a,ll k,int deg=-1){ int n=a.size(); if(deg==-1)deg=n; if(k==0){ vector<T>ret(deg,0); ret[0]=1; return ret; } int of=0; while(a[of]==0&&of!=n)of++; if(of==n)return vector<T>(deg,0); if(of!=0&&k>=deg)return vector<T>(deg,0); if(of*k>=deg)return vector<T>(deg,0); a.erase(a.begin(),a.begin()+of); n=a.size(); T a0=a[0]; T inv=a[0].inv(); rep(i,n)a[i]*=inv; vector<T>lg=fps_log(a,deg); rep(i,deg)lg[i]*=k; vector<T>ep=fps_exp(lg,deg); T pw=a0.pow(k); rep(i,deg)ep[i]*=pw; vector<T>ret(deg,0); reps(i,of*k,deg)ret[i]=ep[i-of*k]; return ret; } template<typename T> vector<T>fps_pows(vector<T>f,vector<T>g,int n){ int sz=f.size(); vector<vector<T>>p(sz,vector<T>(2)),q(sz,vector<T>(2)); q[0][0]=1; rep(i,min<int>(g.size(),sz))p[i][0]=g[i]; rep(i,sz)q[i][1]=-f[i]; int k=sz-1; while(k){ int x=p.size(),y=p[0].size(); vector<vector<T>>mq(q); for(int i=1;i<x;i+=2)rep(j,y)mq[i][j]=-mq[i][j]; vector<vector<T>>f=convolution2d(p,mq); vector<vector<T>>g=convolution2d(q,mq); p.resize(k/2+1); q.resize(k/2+1); if(k&1)rep(i,p.size())p[i]=f[i*2+1]; else rep(i,p.size())p[i]=f[i*2]; rep(i,q.size())q[i]=g[i*2]; rep(i,p.size())if(p[i].size()>n+1)p[i].resize(n+1); rep(i,q.size())if(q[i].size()>n+1)q[i].resize(n+1); k>>=1; } vector<T>f2(p[0]),g2(q[0]); vector<T>ret=ntt_convolution(f2,fps_inv(g2)); ret.resize(n+1); return ret; } template<typename T> vector<T>fps_divmod(vector<T>f,vector<T>g){ if(f.empty())return f; int n=f.size(),m=g.size(); vector<T>r(f); reverse(all(f)),reverse(all(g)); vector<T>q=ntt_convolution(f,fps_inv(g,n)); q.resize(max(0,n-m+1)); auto p=ntt_convolution(g,q); reverse(all(p)); rep(i,min<int>(p.size(),m))r[i]-=p[i]; r.resize(m-1); while(!r.empty()&&r.back().val()==0)r.pop_back(); return r; } template<typename T> vector<T>fps_sqrt(vector<T>f,int deg=-1){ if(deg==-1)deg=f.size(); int prefix_zero=0; while(prefix_zero<f.size()&&f[prefix_zero].val()==0)prefix_zero++; if(prefix_zero==f.size())return vector<T>(deg,0); if(prefix_zero&1)return vector<T>{}; f.erase(f.begin(),f.begin()+prefix_zero); prefix_zero/=2; int sq=sqrt_mod(f[0].val(),T::mod()); if(sq==-1)return vector<T>{}; T inv0=f[0].inv(); rep(i,f.size())f[i]*=inv0; vector<T>g{1}; debug(f); T inv2=T::raw(2).inv(); while(g.size()<deg-prefix_zero){ vector<T>fp(f.begin(),f.begin()+min(f.size(),g.size()*2)); fp=ntt_convolution(fp,fps_inv(g,g.size()*2)); vector<T>nxtg(g); nxtg.resize(g.size()*2); rep(i,nxtg.size()){ if(i<fp.size())nxtg[i]+=fp[i]; nxtg[i]*=inv2; } swap(g,nxtg); } g.insert(g.begin(),prefix_zero,0); g.resize(deg); rep(i,g.size())g[i]*=sq; return g; } template<typename T> std::vector<T>fps_middle_product(std::vector<T>a,std::vector<T>b){ assert(a.size()>=b.size()); std::reverse(b.begin(),b.end()); int n=a.size(),m=b.size(); int z=ceil_pow2(n); a.resize(z,0),b.resize(z,0); dft(a),dft(b); for(int i=0;i<z;i++)a[i]*=b[i]; idft(a); T invz=T(z).inv(); for(int i=m-1;i<n;i++)a[i]*=invz; return vector<T>(a.begin()+m-1,a.begin()+n); } template<typename T> std::vector<T>multipoint_evaluation(std::vector<T>f,const std::vector<T>&x){ int n=ceil_pow2((int)x.size()),log2n=msb(n); std::vector<std::vector<T>>f2(log2n+1,std::vector<T>(n*2)); for(int i=0;i<x.size();i++)f2[0][i*2]=-x[i]; for(int i=0;i<log2n;i++){ int b=1<<i; for(int j=0;j<n*2;j+=b*4){ std::vector<T>l(f2[i].begin()+j,f2[i].begin()+j+b); std::vector<T>r(f2[i].begin()+j+b*2,f2[i].begin()+j+b*3); l.resize(b*2,0),r.resize(b*2,0); ntt_doubling(l),ntt_doubling(r); for(int k=0;k<b;k++)l[k]++,r[k]++; for(int k=b;k<b*2;k++)l[k]--,r[k]--; std::copy(l.begin(),l.end(),f2[i].begin()+j); std::copy(r.begin(),r.end(),f2[i].begin()+j+b*2); for(int k=0;k<b*2;k++)f2[i+1][j+k]=l[k]*r[k]-T::raw(1); } } std::vector<T>g(f2[log2n].begin(),f2[log2n].begin()+n); idft(g); { T inv=T::raw(g.size()).inv(); for(int i=0;i<n;i++)g[i]*=inv; } g.push_back(T::raw(1)); std::reverse(g.begin(),g.end()); g=fps_inv(g,f.size()); f.resize(f.size()+n-1); f=fps_middle_product(f,g); std::reverse(f.begin(),f.end()); transposed_idft(f); { T inv=T::raw(f.size()).inv(); for(int i=0;i<f.size();i++)f[i]*=inv; } for(int i=log2n-1;i>=0;i--){ std::vector<T>nxtf(n); int b=1<<i; for(int j=0;j<n;j+=b*2){ std::vector<T>l(b*2),r(b*2); for(int k=0;k<b*2;k++)l[k]=f[j+k]*f2[i][j*2+b*2+k],r[k]=f[j+k]*f2[i][j*2+k]; transposed_ntt_doubling(l),transposed_ntt_doubling(r); for(int k=0;k<b;k++)nxtf[j+k]=l[k],nxtf[j+b+k]=r[k]; } std::swap(f,nxtf); } f.resize(x.size()); return f; } using mint=mint998; void SOLVE(){ int n; cin>>n; vector<mint>a(n); cin>>a; mint ans=1; auto dfs=[&](auto self,int l,int r)->vector<mint> { if(l+1==r)return {a[l],-1}; int mid=(l+r)/2; vector<mint>pl=self(self,l,mid); vector<mint>pr=self(self,mid,r); vector<mint>x(a.begin()+mid,a.begin()+r); vector<mint>b=multipoint_evaluation(pl,x); for(auto i:b)ans*=i; return ntt_convolution(pl,pr); }; dfs(dfs,0,n); cout<<ans.val()<<endl; }