QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#662878#9493. 路径计数ucup-team100419 4218ms221156kbC++1710.8kb2024-10-21 11:39:402024-10-21 11:39:41

Judging History

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

  • [2024-10-21 11:39:41]
  • 评测
  • 测评结果:19
  • 用时:4218ms
  • 内存:221156kb
  • [2024-10-21 11:39:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include"debug.h"
#else
#define debug(...) void()
#endif
#define all(x) (x).begin(),(x).end()
template<class T>
auto ary(T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
using ll=long long;
using ull=unsigned long long;
using LL=__int128;
const int mod=998244353;
ll qpow(ll x,ll y=mod-2,ll ans=1){
	static const int Mod=mod-1;
	y=(y%Mod+Mod)%Mod;
	for(;y;(x*=x)%=mod,y>>=1)if(y&1)(ans*=x)%=mod;
	return ans;
}
mt19937 rnd(time(0));
int sqr(int n){
	int a,val;
	auto chk=[&](int x){
		return qpow(x,(mod-1)/2)==1;
	};
	do a=rnd()%mod;while(chk(val=(1ll*a*a-n)%mod));
	struct comp{
		int x,y;
		comp(int a=0,int b=0):x(a),y(b){}
		comp operator + (const comp &a)const{
			return comp((x+a.x)%mod,(y+a.y)%mod);
		}
	}x(a,1),ans(1,0);
	auto mul=[&](comp a,comp b){
		return comp((1ll*a.x*b.x+1ll*a.y*b.y%mod*val)%mod,(1ll*a.x*b.y+1ll*a.y*b.x)%mod);
	};
	int y=(mod+1)/2;
	for(;y;x=mul(x,x),y>>=1)if(y&1)ans=mul(ans,x);
	return min(ans.x,mod-ans.x);
}
namespace Poly{
	const int N=1<<21,g=[](){
		vector<int>p;
		int x=mod-1;
		for(int i=2;i*i<=x;i++){
			if(x%i==0)p.push_back(i);
			for(;x%i==0;x/=i);
		}
		if(x>1)p.push_back(x);
		auto chk=[&](int x){
			for(int y:p){
				if(qpow(x,(mod-1)/y)==1)return 0;
			}
			return 1;
		};
		int res=1;
		for(;!chk(res);res++);
		return res;
	}();
	int lim,rev[N],A[N],B[N],pw[N];
	void init(int n){
		for(lim=1;lim<n;lim<<=1);
		for(int i=1;i<lim;i++)rev[i]=rev[i>>1]>>1|(i&1?lim>>1:0);
	}
	void Init(){
		int x=qpow(g,(mod-1)/N);
		pw[N/2]=1;
		for(int i=N/2+1;i<N;i++)pw[i]=1ll*pw[i-1]*x%mod;
		for(int i=N/2-1;i;i--)pw[i]=pw[i<<1];
	}
	struct Init_{
		Init_(){Init();}
	}init_;
	namespace Public{
		using poly=vector<int>;
		poly Ifac(int n){
			poly ifac(n,1);
			for(int i=2;i<n;i++)ifac[i]=1ll*ifac[mod%i]*(mod-mod/i)%mod;
			for(int i=1;i<n;i++)ifac[i]=1ll*ifac[i]*ifac[i-1]%mod;
			return ifac;
		}
		void NTT(int *f,int op){
			static unsigned long long a[N];
			for(int i=0;i<lim;i++)a[i]=f[rev[i]];
			for(int len=1,x;len<lim;len<<=1){
				for(int i=0;i<lim;i+=len<<1){
					for(int j=0;j<len;j++){
						x=a[i|j|len]*pw[len|j]%mod;
						a[i|j|len]=a[i|j]+mod-x,a[i|j]+=x;
					}
				}
				if(len>>16&1){
					for(int i=0;i<lim;i++)a[i]%=mod;
				}
			}
			for(int i=0;i<lim;i++)f[i]=a[i]%mod;
			if(op<0){
				reverse(f+1,f+lim);
				int x=qpow(lim);
				for(int i=0;i<lim;i++)f[i]=1ll*f[i]*x%mod;
			}
		}
		void NTT(poly &f,int op){
			static unsigned long long a[N];
			for(int i=0;i<lim;i++)a[i]=f[rev[i]];
			for(int len=1,x;len<lim;len<<=1){
				for(int i=0;i<lim;i+=len<<1){
					for(int j=0;j<len;j++){
						x=a[i|j|len]*pw[len|j]%mod;
						a[i|j|len]=a[i|j]+mod-x,a[i|j]+=x;
					}
				}
				if(len>>16&1){
					for(int i=0;i<lim;i++)a[i]%=mod;
				}
			}
			for(int i=0;i<lim;i++)f[i]=a[i]%mod;
			if(op<0){
				reverse(f.begin()+1,f.begin()+lim);
				int x=qpow(lim);
				for(int i=0;i<lim;i++)f[i]=1ll*f[i]*x%mod;
			}
		}
		poly operator * (const poly &a,const poly &b){ // poly multiple
			int n=a.size(),m=b.size(),k=n+m-1;
			poly c(k);
			if(min(n,m)<=50){
				for(int i=0;i<n;i++){
					for(int j=0;j<m;j++){
						c[i+j]=(c[i+j]+1ll*a[i]*b[j])%mod;
					}
				}
				return c;
			}
			init(k);
			copy(all(a),A),fill(A+n,A+lim,0);
			copy(all(b),B),fill(B+m,B+lim,0);
			NTT(A,1),NTT(B,1);
			for(int i=0;i<lim;i++)A[i]=1ll*A[i]*B[i]%mod;
			NTT(A,-1);
			for(int i=0;i<k;i++)c[i]=A[i];
			return c;
		}
		poly& operator *= (poly &a,const poly &b){
			return a=a*b;
		}
		poly& operator += (poly &a,const poly &b){
			int n=a.size(),m=b.size();
			if(n<m)a.resize(m);
			for(int i=0;i<m;i++)(a[i]+=b[i])%=mod;
			return a;
		}
		poly operator + (const poly &a,const poly &b){ // poly addition
			poly c(a);
			return c+=b;
		}
		poly& operator -= (poly &a,const poly &b){
			int n=a.size(),m=b.size();
			if(n<m)a.resize(m);
			for(int i=0;i<m;i++)(a[i]+=mod-b[i])%=mod;
			return a;
		}
		poly operator - (const poly &a,const poly &b){ // poly subtraction
			poly c(a);
			return c-=b;
		}
		poly operator - (const poly &a){
			return poly()-a;
		}
		poly& operator *= (poly &a,const int &b){
			for(int &x:a)x=1ll*x*b%mod;
			return a;
		}
		poly operator * (const poly &a,const int &b){
			poly c(a);
			return c*=b;
		}
		poly& operator /= (poly &a,const int &b){
			return a*=qpow(b);
		}
		poly operator / (const poly &a,const int &b){
			poly c(a);
			return c/=b;
		}
		poly& operator %= (poly &a,const int &b){
			if(a.size()>b)a.resize(b);
			return a;
		}
		poly operator % (const poly &a,const int &b){
			poly c(a);
			return c%=b;
		}
		poly inv(poly a,int n=-1){ // A^-1 mod x^n
			if(n==-1)n=a.size();
			if(n==1)return poly(1,1);
			int m=(n+1)/2;
			auto b=inv(a%m,m);
			init(n+m+m-2);
			a.resize(lim),b.resize(lim);
			NTT(a,1),NTT(b,1);
			for(int i=0;i<lim;i++){
				a[i]=(2+1ll*(mod-a[i])*b[i])%mod*b[i]%mod;
			}
			NTT(a,-1);
			return a%n;
		}
	}
}
using namespace Poly::Public;
const int N=2e5+10;
int sid,n,m,p,a[N],b[N],c[N],d[N],e[N],f[N];
using vec=array<poly,2>;
using matrix=array<vec,2>;
#ifdef DEBUG
ostream& operator << (ostream &out,vec a){
	out<<'[';
	for(auto x:a)out<<x<<',';
	return out<<']';
}
ostream& operator << (ostream &out,matrix a){
	out<<'[';
	for(auto x:a)out<<x<<',';
	return out<<']';
}
#endif
matrix operator * (matrix a,matrix b){
	int n=0;
	for(int i:{0,1})for(int j:{0,1})for(int k:{0,1}){
		n=max(n,(int)a[i][j].size()+(int)b[j][k].size()-1);
	}
	matrix c;
	if(n<=50){
		for(int i:{0,1})for(int j:{0,1})for(int k:{0,1}){
			if(!a[i][j].empty()&&!b[j][k].empty())c[i][k]+=a[i][j]*b[j][k];
		}
		return c;
	}
	Poly::init(n),n=Poly::lim;
	for(int i:{0,1})for(int j:{0,1}){
		a[i][j].resize(n),b[i][j].resize(n),c[i][j].resize(n);
		NTT(a[i][j],1),NTT(b[i][j],1);
	}
	for(int i:{0,1})for(int j:{0,1})for(int k:{0,1}){
		for(int x=0;x<n;x++)c[i][k][x]=(c[i][k][x]+1ll*a[i][j][x]*b[j][k][x])%mod;
	}
	for(int i:{0,1})for(int j:{0,1}){
		NTT(c[i][j],-1);
		for(;!c[i][j].empty()&&!c[i][j].back();c[i][j].pop_back());
	}
	return c;
}
matrix mul(matrix a,matrix b){
	matrix c;
	for(int i:{0,1})for(int j:{0,1})for(int k:{0,1}){
		if(!a[i][j].empty()&&!b[j][k].empty())c[i][k]+=a[i][j]*b[j][k];
	}
	return c;
}
vec solve(int l,int r){
	if(l==r){
		// debug("solve",l,r,vec{poly{b[l],1},poly{e[l]}});
		return vec{poly{b[l],1},poly{e[l]}};
	}
	int mid=(l+r)>>1,n=0;
	auto L=solve(l,mid),R=solve(mid+1,r);
	for(int i:{0,1})for(int j:{0,1})if(!i||!j){
		n=max(n,(int)L[i].size()+(int)R[j].size()-1);
	}
	// debug("solve",l,r);
	Poly::init(n),n=Poly::lim;
	vec t;
	L[0].resize(n),NTT(L[0],1);
	for(int i:{0,1}){
		R[i].resize(n),t[i].resize(n);
		NTT(R[i],1);
	}
	for(int i=0;i<n;i++){
		t[0][i]=1ll*L[0][i]*R[0][i]%mod;
		t[1][i]=1ll*L[0][i]*R[1][i]%mod;
	}
	NTT(t[0],-1),NTT(t[1],-1);
	for(int i:{0,1}){
		for(;!t[i].empty()&&!t[i].back();t[i].pop_back());
	}
	t[1]+=L[1];
	// debug("solve",l,r,t);
	return t;
}
struct node{
	matrix F;
	poly G[2][3];
	poly calc(int cr){
		return F[0][0]*poly{(mod-cr)%mod,1}+F[0][1];
	}
};
void expand(poly &a,poly f,poly g,int m){
	int n=a.size();
	if(m<=n)return a%=m,void();
	a=a*f%n*g%m;
}
const int K=500;
matrix gen(int i){
	matrix cur;
	cur[0][0]=poly{(mod-c[i])%mod,1};
	cur[0][1]=poly{int(1ll*(mod-a[i])*d[i+1]%mod)};
	cur[1][0]=poly{1};
	return cur;
}
node divide(int l,int r){
	int n=r-l+1;
	if(r-l+1<=K){
		node res;
		function<matrix(int,int)>dfs=[&](int l,int r){
			if(l==r)return gen(l);
			int mid=(l+r)>>1;
			return dfs(l,mid)*dfs(mid+1,r);
		};
		if(l<r)res.F=dfs(l,r-1);
		else res.F[0][0]=res.F[1][1]=poly{1};
		static int w[K][N];
		for(int x:{0,1}){
			for(int i=0;i<n;i++)fill(w[i]+l,w[i]+1+r,0);
			w[0][x?r:l]=1;
			for(int i=0;i+1<n;i++){
				for(int j=l;j<=r;j++)w[i+1][j]=(w[i+1][j]+1ll*w[i][j]*c[j])%mod;
				for(int j=l;j<r;j++)w[i+1][j+1]=(w[i+1][j+1]+1ll*w[i][j]*a[j])%mod;
				for(int j=r;j>l;j--)w[i+1][j-1]=(w[i+1][j-1]+1ll*w[i][j]*d[j])%mod;
			}
			res.G[x][0].resize(n);
			res.G[x][1].resize(n);
			res.G[x][2].resize(n);
			for(int i=0;i<n;i++){
				res.G[x][0][i]=(res.G[x][0][i]+w[i][l])%mod;
				res.G[x][1][i]=(res.G[x][1][i]+w[i][r])%mod;
				for(int j=l;j<=r;j++){
					res.G[x][2][i]=(res.G[x][2][i]+1ll*w[i][j]*f[j])%mod;
				}
			}
		}
		return res;
	}
	int mid=(l+r)>>1;
	auto L=divide(l,mid-1),R=divide(mid+1,r);
	auto fl=L.calc(c[mid-1]),fr=R.calc(c[r]);
	reverse(all(fl)),reverse(all(fr));
	auto ifl=inv(fl,n),ifr=inv(fr,n);
	for(int i:{0,1})for(int j:{0,1,2}){
		expand(L.G[i][j],fl,ifl,n);
		expand(R.G[i][j],fr,ifr,n);
	}
	node res;
	res.F=mul(L.F,gen(mid-1))*mul(gen(mid),R.F);
	poly h=(poly{0,c[mid]}+
		L.G[1][1]*poly{0,0,int(1ll*d[mid]*a[mid-1]%mod)}+
		R.G[0][0]*poly{0,0,int(1ll*a[mid]*d[mid+1]%mod)})%n;
	h=inv(poly{1}-h,n);
	poly p[2],q[3];
	p[0]=poly{0,a[mid-1]}*L.G[0][1]%n;
	p[1]=poly{0,d[mid+1]}*R.G[1][0]%n;
	q[0]=poly{0,d[mid]}*L.G[1][0]%n;
	q[1]=poly{0,a[mid]}*R.G[0][1]%n;
	q[2]=(poly{0,d[mid]}*L.G[1][2]+poly{0,a[mid]}*R.G[0][2]+poly{f[mid]})%n;
	Poly::init(n+n-1);
	int k=Poly::lim;
	p[0].resize(k),p[1].resize(k),h.resize(k);
	NTT(p[0],1),NTT(p[1],1),NTT(h,1);
	for(int i=0;i<k;i++){
		p[0][i]=1ll*p[0][i]*h[i]%mod;
		p[1][i]=1ll*p[1][i]*h[i]%mod;
	}
	NTT(p[0],-1),NTT(p[1],-1);
	p[0].resize(n),p[1].resize(n);
	p[0].resize(k),p[1].resize(k);
	q[0].resize(k),q[1].resize(k),q[2].resize(k);
	NTT(p[0],1),NTT(p[1],1);
	NTT(q[0],1),NTT(q[1],1),NTT(q[2],1);
	for(int i:{0,1})for(int j:{0,1,2})res.G[i][j].resize(k);
	for(int i=0;i<k;i++){
		res.G[0][0][i]=1ll*p[0][i]*q[0][i]%mod;
		res.G[0][1][i]=1ll*p[0][i]*q[1][i]%mod;
		res.G[0][2][i]=1ll*p[0][i]*q[2][i]%mod;
		res.G[1][0][i]=1ll*p[1][i]*q[0][i]%mod;
		res.G[1][1][i]=1ll*p[1][i]*q[1][i]%mod;
		res.G[1][2][i]=1ll*p[1][i]*q[2][i]%mod;
	}
	for(int i:{0,1})for(int j:{0,1,2}){
		NTT(res.G[i][j],-1),res.G[i][j].resize(n);
	}
	res.G[0][0]+=L.G[0][0];
	res.G[0][2]+=L.G[0][2];
	res.G[1][1]+=R.G[1][1];
	res.G[1][2]+=R.G[1][2];
	return res;
}
int main(){
	Poly::Init();
	scanf("%d%d%d%d",&sid,&n,&m,&p);
	for(int i=0;i<m;i++)scanf("%d",&a[i]);
	for(int i=0;i<n;i++)scanf("%d",&b[i]);
	for(int i=0;i<=m;i++)scanf("%d",&c[i]);
	for(int i=1;i<=m;i++)scanf("%d",&d[i]);
	for(int i=0;i<=n;i++)scanf("%d",&e[i]);
	for(int i=0;i<=m;i++)scanf("%d",&f[i]);
	poly h=solve(0,n)[1];
	h.resize(n+1);
	// debug(h);
	node res=divide(0,m);
	poly f=res.calc(c[m]),g=res.G[0][2];
	reverse(all(f));
	expand(g,f,inv(f,n+1),n+1);
	// debug(f);
	// for(int i:{0,1})for(int j:{0,1,2}){
	// 	poly t;
	// 	for(int x=0;x<res.G[i][j].size()&&x<10;x++)t.push_back(res.G[i][j][x]);
	// 	debug(i,j,t);
	// }
	// debug(g,res.G[0][2],h,f);
	int ans=0;
	for(int i=0;i<=n;i++)ans=(ans+1ll*h[i]*g[i])%mod;
	cout<<ans<<endl;
	return 0;
}
#ifdef DEBUG
#include"debug.hpp"
#endif

详细

Subtask #1:

score: 3
Accepted

Test #1:

score: 3
Accepted
time: 145ms
memory: 34972kb

input:

1 5000 5000 998244353
121811167 379924090 361631583 174189813 559424693 889647308 193102812 469875055 32237660 96186933 624360154 404866088 859165067 748410791 926700198 368632735 476560636 798138824 17883437 712872225 448819400 33122704 572152288 627010263 336722521 775918346 465722630 681224329 60...

output:

698779876

result:

ok single line: '698779876'

Test #2:

score: 3
Accepted
time: 109ms
memory: 60740kb

input:

1 4999 4919 998244353
380477138 780960797 787883474 484131625 121182688 933501638 947532778 257948759 108946829 468475856 5564419 677097370 766236309 678789875 533319074 885579006 769287005 710724535 260200529 599758240 380918586 216469888 78561030 325936496 189815268 268769489 232850763 937923688 9...

output:

475720945

result:

ok single line: '475720945'

Test #3:

score: 3
Accepted
time: 119ms
memory: 46840kb

input:

1 4995 4997 998244353
267305461 291432119 107593765 530653184 893921215 15414240 991509792 601027626 313017049 827685069 650321853 385956762 140661695 303322469 142427516 910941336 265371405 731125266 512689773 723594553 552396112 379799950 43662480 666390792 938399292 144960568 817187890 428532063 ...

output:

568578871

result:

ok single line: '568578871'

Subtask #2:

score: 0
Time Limit Exceeded

Test #4:

score: 0
Time Limit Exceeded

input:

2 200000 200000 998244353
903563506 433239074 632684755 970178226 831892753 932120646 157832416 517140217 296101978 998244343 850946564 2367119 708278025 376919151 752106478 994362560 806760771 672325565 9 958330492 343658496 153627310 330649130 983441587 829707074 135609242 706388812 325297147 4972...

output:


result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #7:

score: 0
Time Limit Exceeded

input:

3 200000 200000 998244353
493605813 622283646 579332647 528537957 211102509 336893985 106292723 166710741 443808575 180234697 744331593 252016663 693453924 975202110 519712748 174749950 250990381 584528219 619047448 641168296 914918741 785005029 433175528 400547992 845115512 278159630 227330862 6407...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #2:

0%

Subtask #5:

score: 0
Skipped

Dependency #3:

0%

Subtask #6:

score: 0
Time Limit Exceeded

Test #16:

score: 0
Time Limit Exceeded

input:

6 200000 200000 998244353
401806059 107033001 530043262 862506025 263940497 48524969 232075248 849682830 420464058 64900333 394986647 954304290 957385577 86269798 579307969 896967626 230611640 527078096 39773429 402432856 495204529 272090833 100466767 562115973 196636941 736050044 580541546 81233872...

output:


result:


Subtask #7:

score: 16
Accepted

Dependency #1:

100%
Accepted

Test #18:

score: 16
Accepted
time: 1059ms
memory: 95392kb

input:

7 200000 20000 998244353
869285118 18064361 457499026 292378234 45941854 157946840 586178769 392380503 830348035 141601898 591275235 100919004 399285791 115436998 586990272 287976693 795031696 836945332 895793688 432697639 828280269 714678411 137052352 516420393 730089484 911809696 350536206 8175289...

output:

809172057

result:

ok single line: '809172057'

Test #19:

score: 16
Accepted
time: 1044ms
memory: 92640kb

input:

7 200000 19810 998244353
828902663 612852451 425506197 233025854 319333441 952074173 717042023 949076193 83452305 763673071 41376007 272593034 356441893 361994505 185035237 266625028 550156798 775373755 727044232 359307823 648601627 384158945 313651668 746883203 321976362 307448064 64684150 4186580 ...

output:

560874322

result:

ok single line: '560874322'

Test #20:

score: 16
Accepted
time: 228ms
memory: 67708kb

input:

7 1000 9996 998244353
233393388 143 900 282 622 588 259300855 18722608 81440606 388097181 802 119 117027019 445 244 220 321165142 193445923 884359675 276 671093341 934763449 844222944 251 190039071 208 521 758 621 722 370522729 731 968984566 656850987 42380471 798 292 324 609 606 400 210 785 816 863...

output:

964458165

result:

ok single line: '964458165'

Test #21:

score: 16
Accepted
time: 713ms
memory: 78892kb

input:

7 200000 9919 998244353
127286538 946789972 892201793 335420070 263695484 170118086 108765927 699100677 678757948 552873985 565640470 952666657 645394702 560901257 95959026 804657734 83330667 185533959 520165719 246753496 769277586 697870667 659358703 318912135 658640680 810790915 492140504 62924551...

output:

269104705

result:

ok single line: '269104705'

Subtask #8:

score: 0
Skipped

Dependency #6:

0%

Subtask #9:

score: 0
Skipped

Dependency #5:

0%

Subtask #10:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #32:

score: 0
Wrong Answer
time: 4218ms
memory: 221156kb

input:

10 100000 100000 856853193
287061150 779504426 21691247 494814415 381655016 536089292 240188530 458291018 332927950 428352746 351529999 258956585 651084688 268523951 338285674 719419445 165275422 367200319 35308342 563282834 763880581 117326555 284413760 207571906 703023023 622478929 212959189 34094...

output:

756256154

result:

wrong answer 1st lines differ - expected: '304611769', found: '756256154'