結果
問題 | No.931 Multiplicative Convolution |
ユーザー |
![]() |
提出日時 | 2020-12-05 21:14:31 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 127 ms / 2,000 ms |
コード長 | 4,735 bytes |
コンパイル時間 | 2,740 ms |
コンパイル使用メモリ | 181,256 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-16 03:50:23 |
合計ジャッジ時間 | 5,370 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 14 |
ソースコード
#define _USE_MATH_DEFINES#include <bits/stdc++.h>using namespace std;//template#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)#define ALL(v) (v).begin(),(v).end()typedef long long int ll;const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}//endtemplate<unsigned mod=998244353>struct fp {using uint=unsigned; uint v;static uint get_mod(){return mod;}int inv() const{int tmp,a=v,b=mod,x=1,y=0;while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);if(x<0){x+=mod;} return x;}fp(ll x=0){init(x%mod+mod);}fp& init(uint x){v=(x<mod?x:x-mod); return *this;}fp operator-()const{return fp()-*this;}fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}fp& operator+=(const fp& x){return init(v+x.v);}fp& operator-=(const fp& x){return init(v+mod-x.v);}fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}fp operator+(const fp& x)const{return fp(*this)+=x;}fp operator-(const fp& x)const{return fp(*this)-=x;}fp operator*(const fp& x)const{return fp(*this)*=x;}fp operator/(const fp& x)const{return fp(*this)/=x;}bool operator==(const fp& x)const{return v==x.v;}bool operator!=(const fp& x)const{return v!=x.v;}}; using Fp=fp<>;template<typename T>struct factorial {vector<T> Fact,Finv,Inv;factorial(int maxx){Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1;rep(i,2,maxx){Fact[i]=Fact[i-1]*i;} Finv[maxx-1]=Fact[maxx-1].inv();for(int i=maxx-1;i>=2;i--){Finv[i-1]=Finv[i]*i; Inv[i]=Finv[i]*Fact[i-1];}}T fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];}T inv(int n){return Inv[n];}T nPr(int n,int r){if(n<0||n<r||r<0)return Fp(0);else return Fact[n]*Finv[n-r];}T nCr(int n,int r){if(n<0||n<r||r<0)return Fp(0);else return Fact[n]*Finv[r]*Finv[n-r];}};template<typename T=Fp,unsigned p=3>struct NTT{vector<T> rt,irt;NTT(int lg=21){unsigned m=T::get_mod()-1; T prt=p;rt.resize(lg); irt.resize(lg);rep(k,0,lg){rt[k]=-prt.pow(m>>(k+2));irt[k]=rt[k].inv();}}void ntt(vector<T>& f,bool inv=0){int n=f.size();if(inv){for(int m=1;m<n;m<<=1){ T w=1;for(int s=0,t=0;s<n;s+=m*2){for(int i=s,j=s+m;i<s+m;i++,j++){auto x=f[i],y=f[j];f[i]=x+y; f[j]=(x-y)*w;} w*=irt[__builtin_ctz(++t)];}} T mul=T(n).inv(); rep(i,0,n)f[i]*=mul;}else{for(int m=n;m>>=1;){ T w=1;for(int s=0,t=0;s<n;s+=m*2){for(int i=s,j=s+m;i<s+m;i++,j++){auto x=f[i],y=f[j]*w;f[i]=x+y; f[j]=x-y;} w*=rt[__builtin_ctz(++t)];}}}}vector<T> mult(const vector<T>& a,const vector<T>& b,bool same=0){if(a.empty() and b.empty())return vector<T>();int n=a.size()+b.size()-1,m=1<<__lg(n*2-1);vector<T> res(m); rep(i,0,a.size()){res[i]=a[i];} ntt(res);if(same)rep(i,0,m)res[i]*=res[i];else{vector<T> c(m); rep(i,0,b.size())c[i]=b[i];ntt(c); rep(i,0,m)res[i]*=c[i];} ntt(res,1); return res;}};ll mod(ll a,ll m){return (a%m+m)%m;}ll mpow(ll a,ll t,ll m){ll res=1;while(t){if(t&1)res=mod(res*a,m);a=mod(a*a,m); t>>=1;} return res;}ll minv(ll a,ll m){ll b=m,u=1,v=0;while(b){ll t=a/b;a-=t*b; swap(a,b);u-=t*v; swap(u,v);} u=mod(u,m); return u;}int get_root(int p){vector<ll> ds; int q=p-1;for(ll x=1;x*x<=q;x++)if(q%x==0){ds.push_back(x);if(x*x!=q)ds.push_back(q/x);}sort(ALL(ds));ds.pop_back();for(int x=1;x<=q;x++){for(auto& d:ds){if(mpow(x,d,p)==1)goto fail;}return x;fail:;} assert(0);}NTT<Fp,3> ntt;int main(){int p; cin>>p;vector<int> x(p),y(p);rep(i,1,p)cin>>x[i];rep(i,1,p)cin>>y[i];if(p==2){cout<<mod(1LL*x[1]*y[1],Fp::get_mod())<<endl;return 0;}int rt=get_root(p);vector<Fp> a(100010),b(100010); int idx=1;rep(i,0,p-1){a[i]=x[idx]; b[i]=y[idx];idx=mod(1LL*idx*rt,p);}auto c=ntt.mult(a,b); vector<Fp> res(p); idx=1;rep(i,0,c.size()){res[idx]+=c[i]; idx=mod(1LL*idx*rt,p);}rep(i,1,p)cout<<res[i].v<<(i==p-1?'\n':' ');return 0;}