QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#447818#8721. 括号序列275307894aAC ✓1541ms39572kbC++146.2kb2024-06-18 20:39:062024-06-18 20:39:07

Judging History

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

  • [2024-06-18 20:39:07]
  • 评测
  • 测评结果:AC
  • 用时:1541ms
  • 内存:39572kb
  • [2024-06-18 20:39: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=5e5+5,M=N*4+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const ll INF=1e18+7;mt19937 rnd(time(0));
#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;}
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);
			poly c(lim);copy(a,a+lim,c.begin());
			return c;
		}
		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 inve(poly A,int n=-1){
			if(n==-1) n=A.size();
			if(n==1) return poly({(int)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 n;
ll f[N],g[N],inv[N],frc[N];
void calc(int l,int r){
	if(l==r){
		g[l]=(g[l]+f[l-1])%mod*inv[l]%mod;
		f[l]=(f[l]+g[l])%mod;
		gdb(f[l]*frc[l]%mod,g[l]*frc[l]%mod);
		return;
	}
	int m=l+r>>1;
	calc(l,m);

	poly f1(f+l,f+m+1),g1(g,g+r-l+1),g2(g,g+r-l+1);
	for(int i=0;i<g1.size();i++) (g1[i]*=i)%=mod;
	g1*=f1;g2*=f1;
	for(int i=0;i<g1.size();i++) {
		if(m<i+l+1&&i+l+1<=r) (g[i+l+1]+=g1[i])%=mod;
		if(m<i+l&&i+l<=r) (f[i+l]+=g2[i])%=mod;
	}

	poly f2(f,f+r-l+1),g3(g+l,g+m+1),g4(g+l,g+m+1);
	for(int i=0;i<g3.size();i++) (g3[i]*=i+l)%=mod;
	g3*=f2;g4*=f2;
	for(int i=0;i<g3.size();i++) {
		if(m<i+l+1&&i+l+1<=r) (g[i+l+1]+=g3[i])%=mod;
		if(m<i+l&&i+l<=r) (f[i+l]+=g4[i])%=mod;
	}

	calc(m+1,r);
}
void calcDP(int n){
	if(!n){
		f[0]=g[0]=1;
		return;
	}
	int m=n>>1;
	calcDP(m);
	calc(m+1,n);
	poly f1(f,f+n+1),g1(g,g+n+1),g2(g,g+n+1);
	for(int i=0;i<g1.size();i++) (g1[i]*=i)%=mod;
	if(m+m+1>n) (g[m+m+1]+=(mod-g1[m])*f[m])%=mod;
	g1*=f1;g2*=f1;
	for(int i=0;i<g1.size();i++){
		if(i+1>n) (g[i+1]+=g1[i])%=mod;
		if(i>n) (f[i]+=g2[i])%=mod;
	}
}
void Solve(){
	int i,j;scanf("%d",&n);
	inv[1]=1;for(i=2;i<=n;i++) inv[i]=(mod-inv[mod%i])*(mod/i)%mod;
	for(frc[0]=i=1;i<=n;i++) frc[i]=frc[i-1]*i%mod;
	
	calcDP(n);

	/*for(i=1;i<=n;i++){
		g[i]=f[i-1];
		for(j=0;j<i-1;j++) g[i]+=f[j]*g[i-j-1]%mod*(i-j-1)%mod;
		g[i]=g[i]%mod*inv[i]%mod;
		for(j=1;j<=i;j++) f[i]+=g[j]*f[i-j]%mod;
		f[i]%=mod;
		gdb(f[i]*frc[i]%mod,g[i]*frc[i]%mod,i);
	}*/
	printf("%lld\n",f[n]*frc[n]%mod);
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4144kb

input:

3

output:

28

result:

ok 1 number(s): "28"

Test #2:

score: 0
Accepted
time: 1ms
memory: 4184kb

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 1ms
memory: 4148kb

input:

2

output:

4

result:

ok 1 number(s): "4"

Test #4:

score: 0
Accepted
time: 1ms
memory: 4192kb

input:

4

output:

282

result:

ok 1 number(s): "282"

Test #5:

score: 0
Accepted
time: 1ms
memory: 4172kb

input:

5

output:

3718

result:

ok 1 number(s): "3718"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3968kb

input:

6

output:

60694

result:

ok 1 number(s): "60694"

Test #7:

score: 0
Accepted
time: 0ms
memory: 4028kb

input:

7

output:

1182522

result:

ok 1 number(s): "1182522"

Test #8:

score: 0
Accepted
time: 0ms
memory: 4028kb

input:

8

output:

26791738

result:

ok 1 number(s): "26791738"

Test #9:

score: 0
Accepted
time: 1ms
memory: 3972kb

input:

9

output:

692310518

result:

ok 1 number(s): "692310518"

Test #10:

score: 0
Accepted
time: 1ms
memory: 4008kb

input:

10

output:

135061370

result:

ok 1 number(s): "135061370"

Test #11:

score: 0
Accepted
time: 1ms
memory: 4072kb

input:

100

output:

423669705

result:

ok 1 number(s): "423669705"

Test #12:

score: 0
Accepted
time: 0ms
memory: 4228kb

input:

1234

output:

878522960

result:

ok 1 number(s): "878522960"

Test #13:

score: 0
Accepted
time: 311ms
memory: 11788kb

input:

54321

output:

827950477

result:

ok 1 number(s): "827950477"

Test #14:

score: 0
Accepted
time: 368ms
memory: 16248kb

input:

65536

output:

380835743

result:

ok 1 number(s): "380835743"

Test #15:

score: 0
Accepted
time: 779ms
memory: 29312kb

input:

131072

output:

842796122

result:

ok 1 number(s): "842796122"

Test #16:

score: 0
Accepted
time: 715ms
memory: 21996kb

input:

131071

output:

981021531

result:

ok 1 number(s): "981021531"

Test #17:

score: 0
Accepted
time: 714ms
memory: 22188kb

input:

131070

output:

480197639

result:

ok 1 number(s): "480197639"

Test #18:

score: 0
Accepted
time: 770ms
memory: 29180kb

input:

131074

output:

383000585

result:

ok 1 number(s): "383000585"

Test #19:

score: 0
Accepted
time: 758ms
memory: 29000kb

input:

131073

output:

316664839

result:

ok 1 number(s): "316664839"

Test #20:

score: 0
Accepted
time: 1497ms
memory: 39344kb

input:

250000

output:

119658643

result:

ok 1 number(s): "119658643"

Test #21:

score: 0
Accepted
time: 1541ms
memory: 39344kb

input:

249999

output:

78110138

result:

ok 1 number(s): "78110138"

Test #22:

score: 0
Accepted
time: 1528ms
memory: 39572kb

input:

249998

output:

297253469

result:

ok 1 number(s): "297253469"

Extra Test:

score: 0
Extra Test Passed