【LOJ #6067】「2017 山东一轮集训 Day3」第三题(MTT / 多项式多点求值)

传送门


我管你什么推式子

莽一个mttmtt就完了

InvInv那里实现的好就可以跑到1s1s一个点

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e6+3;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
typedef vector<int> poly;
namespace Poly{
	cs int C=18,M=(1<<C)|1,T=1023;
	struct plx{
		double x,y;
		plx(double _x=0,double _y=0):x(_x),y(_y){}
		friend inline plx operator +(cs plx &a,cs plx &b){
			return plx(a.x+b.x,a.y+b.y);
		}
		friend inline plx operator -(cs plx &a,cs plx &b){
			return plx(a.x-b.x,a.y-b.y);
		}
		friend inline plx operator *(cs plx &a,cs plx &b){
			return plx(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
		}
		friend inline plx operator /(cs plx &a,cs int &b){
			return plx(a.x/b,a.y/b);
		}
		inline plx conj()cs{return plx(x,-y);}
	};
	plx *w[C+1];
	int rev[M];
	cs double pi=acos(-1);
	inline void init_rev(int lim){
		for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
	}
	inline void init_w(){
		for(int i=1;i<=C;i++)w[i]=new plx[(1<<(i-1))|1];
		w[C][0]=plx(1,0);
		for(int i=1,l=(1<<(C-1));i<l;i++)w[C][i]=plx(cos(pi*i/l),sin(pi*i/l));
		for(int i=C-1;i;i--)
		for(int j=0,l=1<<(i-1);j<l;j++)w[i][j]=w[i+1][j<<1];
	}
	inline void fft(plx *f,int lim,int kd){
		for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
		plx a0,a1;
		for(int mid=1,l=1;mid<lim;mid<<=1,l++)
		for(int i=0;i<lim;i+=mid<<1)
		for(int j=0;j<mid;j++)
		a0=f[i+j],a1=f[i+j+mid]*w[l][j],f[i+j]=a0+a1,f[i+j+mid]=a0-a1;
		if(kd==-1){
			reverse(f+1,f+lim);
			for(int i=0;i<lim;i++)f[i]=f[i]/lim;
		}
	}
	inline poly operator +(poly a,cs poly &b){
		a.resize(max(a.size(),b.size()));
		for(int i=0;i<a.size();i++)Add(a[i],b[i]);
		return a;
	}
	inline poly operator -(poly a,cs poly &b){
		a.resize(max(a.size(),b.size()));
		for(int i=0;i<a.size();i++)Dec(a[i],b[i]);
		return a;
	}
	inline poly operator *(poly A,poly B){
		static plx a[M],b[M],c[M],d[M],da,db,dc,dd;
		int deg=A.size()+B.size()-1;
		if(deg<=32){
			poly C(deg,0);
			for(int i=0;i<A.size();i++)
			for(int j=0;j<B.size();j++)
			Add(C[i+j],mul(A[i],B[j]));
			return C;
		}
		int lim=1;
		while(lim<deg)lim<<=1;
		init_rev(lim);
		for(int i=0;i<A.size();i++)a[i]=plx(A[i]&T,A[i]>>10);
		for(int i=A.size();i<lim;i++)a[i]=plx();
		for(int i=0;i<B.size();i++)b[i]=plx(B[i]&T,B[i]>>10);
		for(int i=B.size();i<lim;i++)b[i]=plx();
		fft(a,lim,1),fft(b,lim,1);
		for(int i=0;i<lim;i++){
			int j=(lim-i)&(lim-1);
			da=(a[i]+a[j].conj())*plx(0.5,0);
			db=(a[j].conj()-a[i])*plx(0,0.5);
			dc=(b[i]+b[j].conj())*plx(0.5,0);
			dd=(b[j].conj()-b[i])*plx(0,0.5);
			c[i]=(da*dc)+(da*dd)*plx(0,1);
			d[i]=(db*dc)+(db*dd)*plx(0,1);
		}
		fft(c,lim,-1),fft(d,lim,-1);
		poly res(deg,0);
		for(int i=0;i<deg;i++){
			ll da=(ll)(d[i].x+0.5)%mod,db=(ll)(d[i].y+0.5)%mod,dc=(ll)(c[i].x+0.5)%mod,dd=(ll)(c[i].y+0.5)%mod;
			res[i]=((da<<10)+(db<<20)+(dc)+(dd<<10))%mod;
		}
		return res;
	}
	inline poly Inv(poly a,int deg){
		poly b(1,::Inv(a[0])),c;
		for(int lim=1;lim<deg;lim<<=1){
			c=a,c.resize(lim<<1);
   	 		c=poly(1,2)-c*b,c.resize(lim<<1);
    		b=b*c,b.resize(lim<<1);
		}
		b.resize(deg);return b;
	}
	inline poly operator /(poly a,poly b){
		int lim=1,deg=a.size()-b.size()+1;
		reverse(a.bg(),a.end());
		reverse(b.bg(),b.end());
		while(lim<deg)lim<<=1;
		b=Inv(b,lim),b.resize(deg);
		a=a*b,a.resize(deg);
		reverse(a.bg(),a.end());
		return a;
	}
	inline poly operator %(poly a,poly b){
		if(a.size()<b.size())return a;
		a=a-(a/b)*b,a.resize(b.size()-1);return a;
	}
	#define lc (u<<1)
	#define rc ((u<<1)|1)
	#define mid ((l+r)>>1)
	poly f[M<<2];int x[M];
	void build(int u,int l,int r,int *a){
		if(l==r){f[u].resize(2),f[u][0]=dec(0,a[l]),f[u][1]=1,x[l]=a[l];return;}
		build(lc,l,mid,a),build(rc,mid+1,r,a);
		f[u]=f[lc]*f[rc];
	}
	void calc(int u,int l,int r,poly now,int *v){
		if(r-l+1<=256){
			for(int i=l;i<=r;i++){
				int res=0;
				for(int j=0,mt=1;j<now.size();j++,Mul(mt,x[i]))Add(res,mul(mt,now[j]));
				v[i]=res;
			}
			return;
		}
		calc(lc,l,mid,now%f[lc],v),calc(rc,mid+1,r,now%f[rc],v);
	}
	#undef lc
	#undef rc
	#undef mid
}
using Poly::init_w;
using Poly::build;
using Poly::calc;
cs int N=80005;
int x[N],y[N];
int n,a,b,c,d,e;
poly f;
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	init_w();
	n=read(),b=read(),c=read(),d=read(),e=read();
	for(int i=0;i<n;i++)x[i]=add(add(mul(b,ksm(c,4*i)),mul(d,ksm(c,2*i))),e);
	build(1,0,n-1,x);
	for(int i=0;i<n;i++)f.pb(read());
	calc(1,0,n-1,f,y);
	for(int i=0;i<n;i++)cout<<y[i]<<"\n";
	return 0;
}
posted @ 2020-02-14 22:02  Stargazer_cykoi  阅读(143)  评论(0编辑  收藏  举报