QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#667826#9493. 路径计数275307894a3 106ms26844kbC++146.7kb2024-10-23 08:29:062024-10-23 08:29:15

Judging History

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

  • [2024-10-23 08:29:15]
  • 评测
  • 测评结果:3
  • 用时:106ms
  • 内存:26844kb
  • [2024-10-23 08:29:06]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=2e5+5,M=N*120+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;

using poly=vector<ll>;
ll mpow(ll x,int y=mod-2){ll ans=1;while(y) y&1&&(ans=ans*x%mod),y>>=1,x=x*x%mod;return ans;}
ll sgn(int x){return x&1?mod-1:1;}
namespace Poly{
	const int N=1<<21,g=3;
	int tr[N],k,a[N],b[N];ll pw[N];
	void init(int n){
		for(k=2;k<=n;k<<=1);
		for(int i=0;i<k;i++) tr[i]=(tr[i>>1]>>1)|(i&1?k/2:0);
		pw[k/2]=1;pw[k/2+1]=mpow(g,(mod-1)/k);
		for(int i=k/2+2;i<k;i++) pw[i]=pw[i-1]*pw[k/2+1]%mod;
		for(int i=k/2-1;i;i--) pw[i]=pw[i<<1];
	}
	void ntt(int *A,int k,int flag){
		static ull a[N];for(int i=0;i<k;i++) a[tr[i]]=A[i];
		for(int i=2;i<=k;i<<=1){
			ll *e=pw+i/2;
			for(int j=0;j<k;j+=i){
				for(int h=j;h<j+i/2;h++){
					int x=a[h+i/2]*e[h-j]%mod;
					a[h+i/2]=a[h]+mod-x;a[h]+=x;
				}
			}
			if(i==(1<<18)){
				for(int j=0;j<k;j++) a[j]%=mod;
			}
		}
		for(int i=0;i<k;i++) A[i]=a[i]%mod;
		if(flag) return;
		reverse(A+1,A+k);
		ll iv=mpow(k);for(int i=0;i<k;i++) A[i]=A[i]*iv%mod;
	}
	namespace Public{
		poly operator *(const poly &A,const poly &B){
			int n=A.size(),m=B.size(),lim=n+m-1;
			init(lim);
			copy(A.begin(),A.end(),a);fill(a+n,a+k,0);
			copy(B.begin(),B.end(),b);fill(b+m,b+k,0);
			ntt(a,k,1);ntt(b,k,1);
			for(int i=0;i<k;i++) a[i]=1ll*a[i]*b[i]%mod;
			ntt(a,k,0);
			return poly(a,a+lim);
		}
		poly& operator *=(poly &A,const poly &B){
			return A=A*B;
		}
		poly operator /(const poly &A,const poly &B){
			poly C=A,D=B;
			reverse(D.begin(),D.end());
			C*=D;
			return poly(C.begin()+int(D.size())-1,C.end());
		}
		poly& operator /=(poly &A,const poly &B){
			return A=A/B;
		}
		poly operator *(const poly &A,const int B){
			poly C=A;for(int i=0;i<C.size();i++) C[i]=1ll*C[i]*B%mod;
			return C;
		}
		poly& operator *=(poly &A,const int &B){
			return A=A*B;
		}
		poly operator +(const poly &A,const poly &B){
			poly C(max(A.size(),B.size()));
			for(int i=0;i<A.size();i++) C[i]=A[i];
			for(int i=0;i<B.size();i++) C[i]=(B[i]+C[i])%mod;
			return C;
		}
		poly& operator +=(poly &A,const poly &B){
			return A=A+B;
		}
		poly operator -(const poly &A,const poly &B){
			poly C(max(A.size(),B.size()));
			for(int i=0;i<A.size();i++) C[i]=A[i];
			for(int i=0;i<B.size();i++) C[i]=(C[i]+mod-B[i])%mod;
			return C;
		}
		poly& operator -=(poly &A,const poly &B){
			return A=A-B;
		}
		poly operator %(const poly &A,const int &B){
			poly C=A;
			if(C.size()>B) C.resize(B);
			return C;
		}
		poly& operator %=(poly &A,const int B){
			return A=A%B;
		}
		poly operator <<(const poly &A,const int &B){
			poly C(A.size()+B);
			copy(all(A),C.begin()+B);
			return C;
		}
		poly& operator <<=(poly &A,const int &B){
			return A=A<<B;
		}
		poly operator >>(const poly &A,const int &B){
			if(B>=A.size()) return poly({});
			return poly(A.begin()+B,A.end());
		}
		poly& operator >>=(poly &A,const int &B){
			return A=A>>B;
		}
		poly inve(poly A,int n=-1){
			if(n==-1) n=A.size();
			if(n==1) return poly({mpow(A[0])});
			poly A0=inve(A%(n+1>>1),n+1>>1);
			return A0*(poly({2})-A*A0%n)%n;
		}
		poly sqrt(poly A,int n=-1){
			if(n==-1) n=A.size();
			if(n==1) return poly({1});
			poly A0=sqrt(A%(n+1>>1),n+1>>1);
			return (A0*A0%n+A)*inve(A0,n)%n*(mod+1>>1);
		}
		poly der(poly A){
			poly B(A.size()-1);
			for(int i=1;i<A.size();i++) B[i-1]=1ll*A[i]*i%mod;
			return B;
		}
		poly integ(poly A){
			poly B(A.size()+1);
			static ll inv[N];inv[1]=1;for(int i=2;i<=A.size();i++) inv[i]=(mod-inv[mod%i])*(mod/i)%mod;
			for(int i=0;i<A.size();i++) B[i+1]=inv[i+1]*A[i]%mod;
			return B;
		}
		poly ln(poly A,int n=-1){
			if(n==-1) n=A.size();
			return integ(der(A)*inve(A,n)%n)%n;
		}
		poly exp(poly A,int n=-1){
			if(n==-1) n=A.size();
			if(n==1) return poly({1});
			poly A0=exp(A%(n+1>>1),n+1>>1);
			return A0*(poly({1})-ln(A0,n)+A)%n;
		}
		poly mpow(poly A,int n,ll k){gdb(n,k);
			auto p=find_if(A.begin(),A.end(),[](ll x){return x;})-A.begin();
			if(p==A.size()) return A;
			poly B(A.size()-p);for(int i=p;i<A.size();i++) B[i-p]=A[i];
			ll v=::mpow(B[0],k%Mod),iv=::mpow(B[0]);
			for(ll &i:B) i=i*iv%mod;B=ln(B,n);
			for(ll &i:B) i=k%mod*i%mod;B=exp(B,n);
			for(ll &i:B) i=i*v%mod;
			for(int i=n-1;~i;i--) B[i]=(i>=1ll*k*p?B[i-k*p]:0);
			return B;
		}
	}
}using namespace Poly::Public;


int c,n,m,p;
poly A,B,C,D,E,F;
namespace Sub1{
	const int N=2e5+5;
	ull f[N],g[N];
	void Solve(){
		f[0]=1;
		ull ans=E[0]*F[0]%mod;
		ans=E[0]*F[0];
		for(int i=0;i<n;i++){
			copy(f,f+m+1,g);
			fill(f,f+m+1,0);
			for(int j=0;j<=m;j++){
				if(j<m) (f[j+1]+=g[j]*A[j])%=mod;
				(f[j]+=g[j]*C[j])%=mod;
				if(j) (f[j-1]+=g[j]*D[j])%=mod;
			}
			ull tot=0;
			for(int j=0;j<=m;j++) tot+=f[j]*F[j]%mod;
			ans+=tot%mod*E[i+1]%mod;
		}
		printf("%llu\n",ans%mod);
	}
}

namespace MakeB{
	#define ls v<<1
	#define rs v<<1|1
	pair<poly,poly> dfs(int v=1,int l=0,int r=n){
		if(l==r) return make_pair(poly({E[l]}),l==n?poly({1}):poly({1,B[l]}));
		int m=l+r>>1;
		auto [f1,g1]=dfs(ls,l,m);
		auto [f2,g2]=dfs(rs,m+1,r);
		return make_pair(f1+(f2<<m-l+1)/g1,g1*g2);
	}
	void build(){
		E=dfs().fi;
	}
	#undef ls
	#undef rs
}
void Solve(){
	scanf("%d%d%d%d",&c,&n,&m,&p);
	A.resize(m);B.resize(n);C.resize(m+1);D.resize(m+1);E.resize(n+1);F.resize(m+1);
	for(int i=0;i<m;i++) scanf("%lld",&A[i]);
	for(int i=0;i<n;i++) scanf("%lld",&B[i]);
	for(int i=0;i<=m;i++) scanf("%lld",&C[i]);
	for(int i=1;i<=m;i++) scanf("%lld",&D[i]);
	for(int i=0;i<=n;i++) scanf("%lld",&E[i]);
	for(int i=0;i<=m;i++) scanf("%lld",&F[i]);
	MakeB::build();
	for(int i=0;i<=n;i++) gdb(E[i]);
	Sub1::Solve();
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

详细

Subtask #1:

score: 3
Accepted

Test #1:

score: 3
Accepted
time: 106ms
memory: 26844kb

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: 97ms
memory: 18712kb

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: 102ms
memory: 18660kb

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: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #18:

score: 0
Time Limit Exceeded

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:


result:


Subtask #8:

score: 0
Skipped

Dependency #6:

0%

Subtask #9:

score: 0
Skipped

Dependency #5:

0%

Subtask #10:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #32:

score: 0
Time Limit Exceeded

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:


result: