QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#91438#622. 多项式多点求值abcdeffa#80 1481ms127340kbC++205.4kb2023-03-28 21:33:322023-03-28 21:33:34

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-28 21:33:34]
  • 评测
  • 测评结果:80
  • 用时:1481ms
  • 内存:127340kb
  • [2023-03-28 21:33:32]
  • 提交

answer

#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define mo 998244353
#define N 2400005
#define ad(x,y) x+=y,x-=(x>=mo?mo:0)
using namespace std;
inline char gc()
{
	static char buf[1048576],*p1,*p2;
 	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1048576,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
	char ch=gc();int r=0,w=1;
	for(;ch<'0'||ch>'9';ch=gc())if(ch=='-')w=-1;
	for(;'0'<=ch&&ch<='9';ch=gc())r=r*10+(ch-'0');
	return r*w;
}
inline void write(int x)
{
	if(!x)return putchar('0'),putchar(' '),void(); 
	char ch[15];ch[0]=' ';
	int tot=0;
	while(x)ch[++tot]=x%10+48,x/=10;
	fd(i,tot,0)putchar(ch[i]);
}
int r[N],ginv[N],yg[25],iyg[25],jc[N],inv[N],zq[N];
int ksm(int x,int y)
{
	if(!y)return 1;
	int z=ksm(x,y>>1);
	return((y&1)?1ll*z*z%mo*x%mo:1ll*z*z%mo);
}
int C(int x,int y)
{
	return 1ll*jc[x]*inv[y]%mo*inv[x-y]%mo;
}
struct poly{
	vector<int> a;
	void ntt(int x,int inv)
	{
		fo(i,0,x-1)if(i>(r[i]=(i&1)*x+r[i>>1]>>1))swap(a[i],a[r[i]]);
		for(int len=2,ct=1;len<=x;len<<=1,ct++)
		{
			int mi=len>>1,zqr=(inv<0?iyg[ct]:yg[ct]);
			zq[1]=1;fo(k,2,mi)zq[k]=1ll*zq[k-1]*zqr%mo; 
			for(int wz=0;wz<x;wz+=len)
			{
				fo(k,1,mi)
				{
					int ji=1ll*a[wz+mi+k-1]*zq[k]%mo;
					a[wz+mi+k-1]=a[wz+k-1];
					ad(a[wz+mi+k-1],mo-ji);
					ad(a[wz+k-1],ji);
				}
			}
		}
		if(inv<0)
		{
			int nm=ksm(x,mo-2);
			fo(i,0,x-1)a[i]=1ll*a[i]*nm%mo;
		}
	}
	void dao()
	{
		fo(i,1,a.size()-1)a[i-1]=1ll*a[i]*i%mo;
		a[a.size()-1]=0;
	}
	void jifen()
	{
		fd(i,a.size()-1,1)a[i]=1ll*a[i-1]*ginv[i]%mo;
		a[0]=0;
	}
	void res(int x)
	{
		a.resize(x);
	}
	friend poly operator *(poly A,poly B)
	{
		int x=1,tp=A.a.size()+B.a.size()-1;
		while(x<tp)x<<=1;
		A.res(x),B.res(x);
		A.ntt(x,1);B.ntt(x,1);
		fo(i,0,x-1)A.a[i]=1ll*A.a[i]*B.a[i]%mo;
		A.ntt(x,-1);
		A.res(tp);
		return A;
	}
	friend poly operator +(poly A,poly B)
	{
		int sz=max(A.a.size(),B.a.size());
		A.res(sz),B.res(sz);
		fo(i,0,sz-1)ad(A.a[i],B.a[i]);
		return A;
	}	
	friend poly operator -(poly A,poly B)
	{
		int sz=max(A.a.size(),B.a.size());
		A.res(sz),B.res(sz);
		fo(i,0,sz-1)ad(A.a[i],mo-B.a[i]);
		return A;
	}
	poly dinv(poly A)
	{
		int x=1,sz=A.a.size();
		while(x<sz)x<<=1;
		poly G,B=A;
		B.res(x),G.res(1);
		G.a[0]=ksm(A.a[0],mo-2);
		for(int i=2;i<=x;i<<=1)
		{
			int lenn=i<<1;
			A.res(lenn);G.res(lenn);
			fo(ii,0,i-1)A.a[ii]=B.a[ii];
			fo(ii,i,lenn-1)A.a[ii]=G.a[ii]=0;
			G.ntt(lenn,1);A.ntt(lenn,1);
			fo(ii,0,lenn-1)G.a[ii]=1ll*G.a[ii]*(2-1ll*G.a[ii]*A.a[ii]%mo+mo)%mo;
			G.ntt(lenn,-1);
			fo(ii,i,lenn-1)G.a[ii]=0; 
		}
		G.res(sz);
		return G;
	}
	poly dln(poly A)
	{
		poly G=A;
		int sz=A.a.size();
		A=dinv(A);
		G.dao();
		A=A*G;
		A.jifen();
		A.res(sz);
		return A;
	}
	poly dexp(poly A)
	{	
		int x=1,sz=A.a.size();
		while(x<sz)x<<=1;
		poly s1,s;
		s1.res(1);
		s1.a[0]=1;
		for(int nw=2;nw<=x;nw<<=1)
		{
			s=s1;s.res(nw);
			s=dln(s);
			s=A-s;
			s.res(nw); 
			s.a[0]++;
			s1=s1*s;
		}
		s1.res(sz);
		return s1;
	}
	poly dsqrt(poly A)
	{
		int x=1;
		while(x<A.a.size())x<<=1;
		poly s1,s;A.res(x);
		s1.a.push_back(1);
		for(int nw=1;nw<=x;nw<<=1)
		{
			int lenn=nw<<1;
			s.res(nw);
			fo(i,0,nw-1)s.a[i]=A.a[i];
			s=s*dinv(s1);
			s1.res(lenn);
			fo(i,0,lenn-1)s1.a[i]=1ll*(s1.a[i]+s.a[i])*499122177%mo;
		}
		s1.res(A.a.size());
		return s1;
	}
	poly dpow(poly A,long long x,int x1)
	{
		int wz=0,szz=A.a.size();
		while(!A.a[wz])wz++;
		int nu=A.a[wz],inu=ksm(nu,mo-2);
		fo(i,wz,szz-1)A.a[i-wz]=1ll*A.a[i]*inu%mo;
		A.res(szz-wz);
		A=A.dln(A);
		fo(i,0,A.a.size()-1)A.a[i]=x%mo*A.a[i]%mo;
		A=A.dexp(A);
		nu=ksm(nu,x1);
		fo(i,0,A.a.size()-1)A.a[i]=1ll*A.a[i]*nu%mo;
		A.res(szz);
		if(1ll*wz*x>=szz)
		{
			A.res(szz);
			fo(i,0,szz-1)A.a[i]=0;
			return A; 
		}
		fd(i,szz,0)if(i+(wz)*x>=szz)continue;
		else A.a[i+wz*x]=A.a[i];
		fo(i,0,wz*x-1)A.a[i]=0;
		return A;
	}
	friend poly operator /(poly f,poly q)
	{
		int n=f.a.size()-1,m=q.a.size()-1;
		poly fr=f,qr=q;
		reverse(fr.a.begin(),fr.a.end());
		reverse(qr.a.begin(),qr.a.end());
		fr.res(n-m+1);qr.res(n-m+1);
		poly ret=fr*qr.dinv(qr);
		ret.res(n-m+1);
		reverse(ret.a.begin(),ret.a.end());
		return ret;
	}
	friend poly operator %(poly f,poly q)
	{
		if(f.a.size()<q.a.size())f.res(q.a.size());
		poly nu=f-f/q*q;
		nu.res(q.a.size()-1);
		return nu; 
	}
	void wri()
	{
		for(auto xx:a)write(xx);puts("");
	}
};
void preinv()
{
	ginv[0]=ginv[1]=jc[0]=1;
	fo(i,1,N-5)jc[i]=1ll*jc[i-1]*i%mo;
	inv[N-5]=ksm(jc[N-5],mo-2);
	fd(i,N-5,1)inv[i-1]=1ll*inv[i]*i%mo;
	fo(i,2,N-5)ginv[i]=1ll*ginv[mo%i]*(mo-mo/i)%mo;
	fo(i,1,22)yg[i]=ksm(3,(mo-1)>>i),iyg[i]=ksm(yg[i],mo-2);
}
#define k1 k<<1
#define k2 (k1)+1
poly nu[3000005];
int b[N];
void solve1(int l,int r,int k)
{
	if(l==r)return nu[k].a={mo-b[l],1},void();
	int mid=l+r>>1;
	solve1(l,mid,k1);solve1(mid+1,r,k2);
	nu[k]=nu[k1]*nu[k2];
}
void solve2(int l,int r,int k,poly nw)
{
	if(l==r)return nw.wri(),void();
	int mid=l+r>>1;
	solve2(l,mid,k1,nw%nu[k1]);
	solve2(mid+1,r,k2,nw%nu[k2]);
}
int main()
{
	preinv();
	int n=read()-1,m=read();
	poly a;
	fo(i,0,n)a.a.push_back(read());
	fo(i,1,m)b[i]=read();
	solve1(1,m,1);
	if(n>=m)a=a%nu[1];
	solve2(1,m,1,a);
	return 0;
}

详细

Test #1:

score: 20
Accepted
time: 45ms
memory: 105872kb

input:

100 94
575336069 33153864 90977269 80162592 25195235 334936051 108161572 14050526 356949084 797375084 805865808 286113858 995555121 938794582 458465004 379862532 563357556 293989886 273730531 13531923 113366106 126368162 405344025 443053020 475686818 734878619 338356543 661401660 834651229 527993675...

output:

940122667 
397187437 
905033404 
346709388 
146347009 
49596361 
125616024 
966474950 
693596552 
162411542 
248699477 
217639076 
254290825 
963991654 
951375739 
431661136 
587466850 
933820245 
135676159 
683994808 
821695954 
675479292 
463904298 
15085475 
183389374 
976945620 
668527277 
98940...

result:

ok 94 numbers

Test #2:

score: 20
Accepted
time: 98ms
memory: 104844kb

input:

5000 4999
410683245 925831211 726803342 144364185 955318244 291646122 334752751 893945905 484134283 203760731 533867267 813509277 491860093 413174124 584582617 594704162 976489328 978500071 196938934 628117769 169796671 858963950 562124570 582491326 647830593 238623335 20782490 674939336 656529076 2...

output:

683038054 
713408290 
776843174 
52275065 
602085453 
905088100 
991748340 
720305324 
831850056 
296147844 
79380797 
882313010 
941965313 
987314872 
363655479 
380838721 
51243733 
165728533 
592641557 
679475455 
651115426 
60492203 
787012426 
247557193 
136399242 
484592897 
908383514 
7352758...

result:

ok 4999 numbers

Test #3:

score: 20
Accepted
time: 386ms
memory: 111124kb

input:

30000 29995
536696866 881441742 356233606 594487396 991820796 695996817 7219464 149265950 843761437 329761701 260625152 80366362 598729314 133794090 12808683 67477659 320740422 878134577 879383179 940923483 660160621 18082378 886078389 524050341 35092018 137623841 988429688 258507355 138475726 75726...

output:

319541931 
71523627 
374970852 
25715597 
36244629 
300490421 
920015579 
97305810 
949802809 
507599156 
733158280 
569689405 
234576135 
266469534 
141265915 
989761030 
221701009 
895284489 
707865101 
547950933 
844193939 
688358883 
642066256 
113618699 
877631874 
804170817 
455115375 
4762162...

result:

ok 29995 numbers

Test #4:

score: 20
Accepted
time: 1481ms
memory: 127340kb

input:

100000 99989
703908936 826436271 431732352 607460686 960390248 897906950 506352459 662618885 172508812 713410533 704313866 156459539 879660919 98030681 46358006 400134234 121190289 498201666 616888945 210891377 39623412 687350951 269444705 980768130 381802923 553892268 644461704 287608268 554761733 ...

output:

135579851 
646286631 
74078131 
432534100 
405499800 
291350098 
736555983 
833523488 
132230969 
377599489 
208993791 
503865639 
149603681 
279216057 
477463117 
247401241 
643979698 
478954375 
436185030 
471378650 
234144621 
390722547 
788177217 
69823556 
516048238 
562200936 
507083023 
20149...

result:

ok 99989 numbers

Test #5:

score: 0
Time Limit Exceeded

input:

1000000 999998
326289172 459965021 432610030 381274775 890620650 133203219 755508578 820410129 100497878 978894337 34545975 484258543 341383383 556328539 705716773 985485996 201697555 806763870 456757110 445252781 501965590 655584951 516373423 475444481 554722275 106826011 433893131 385018453 687541...

output:

606800783 
817370938 
237396973 
847847757 
644743839 
247401674 
83642110 
430778992 
481194348 
734716471 
284931123 
740118779 
149843259 
354488296 
948569346 
267724213 
148740992 
487034502 
874993826 
268358083 
945290837 
793539526 
571516423 
380887985 
556022428 
430319434 
939322 
4561328...

result: