QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#417563#8721. 括号序列AfterlifeTL 1067ms14300kbC++206.3kb2024-05-22 19:44:052024-05-22 19:44:06

Judging History

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

  • [2024-05-22 19:44:06]
  • 评测
  • 测评结果:TL
  • 用时:1067ms
  • 内存:14300kb
  • [2024-05-22 19:44:05]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int mod=998244353;
inline int inc(int x,int y){return x+y>=mod?x+y-mod:x+y;}
inline int dec(int x,int y){return x-y<0?x-y+mod:x-y;}
inline int mul(int x,int y){return (ll)x*y%mod;}
inline int qpow(int x,int y){
	int res=1;
	for(;y;y>>=1)res=y&1?mul(res,x):res,x=mul(x,x);
	return res;
}
inline int inv(int x){return qpow(x,mod-2);}

const int N=1<<20;
namespace NTT{
int re[N],w[2][N];
inline int getre(int n){
	int len=1,bit=0;
	while(len<n)len<<=1,++bit;
	for(int i=1;i<len;++i)re[i]=(re[i>>1]>>1)|((i&1)<<(bit-1));
	w[0][0]=w[1][0]=1,w[0][1]=qpow(3,(mod-1)/len),w[1][1]=inv(w[0][1]);
	for(int o=0;o<2;++o)for(int i=2;i<len;++i)
		w[o][i]=mul(w[o][i-1],w[o][1]);
	return len;
}
inline void NTT(int* a,int n,int o=0){
	for(int i=1;i<n;++i)if(i<re[i])swap(a[i],a[re[i]]);
	int R;
	for(int k=1;k<n;k<<=1)
		for(int i=0,t=k<<1,st=n/t;i<n;i+=t)
			for(int j=0,nw=0;j<k;++j,nw+=st)
				R=mul(a[i+j+k],w[o][nw]),a[i+j+k]=dec(a[i+j],R),a[i+j]=inc(a[i+j],R);
	if(o){
		R=inv(n);
		for(int i=0;i<n;++i)a[i]=mul(a[i],R);
	}
}

int t0[N],t1[N],t2[N];
inline void multi(const int* a,const int* b,int* c,int n,int m){
	if(n+m<=32){
		memset(c,0,sizeof(int)*(n+m+1));
		for(int i=0;i<=n;++i)
			for(int j=0;j<=m;++j)
				c[i+j]=inc(c[i+j],mul(a[i],b[j]));
		return;
	}
	int len=getre(n+m+1);
	memset(t0,0,sizeof(int)*len),memcpy(t0,a,sizeof(int)*(n+1));
	memset(t1,0,sizeof(int)*len),memcpy(t1,b,sizeof(int)*(m+1));
	NTT(t0,len),NTT(t1,len);
	for(int i=0;i<len;++i)t0[i]=mul(t0[i],t1[i]);
	NTT(t0,len,1);
	memcpy(c,t0,sizeof(int)*(n+m+1));
}
inline void inver(const int* a,int* b,int n){
	int len=1;
	while(len<=n)len<<=1;
	memset(t0,0,sizeof(int)*len),memcpy(t0,a,sizeof(int)*(n+1));
	memset(t1,0,sizeof(int)*(len<<1));
	memset(t2,0,sizeof(int)*(len<<1));
	t2[0]=inv(t0[0]);
	for(int k=1;k<=len;k<<=1){
		memcpy(t1,t0,sizeof(int)*k);
		getre(k<<1);
		NTT(t1,k<<1),NTT(t2,k<<1);
		for(int i=0;i<(k<<1);++i)t2[i]=mul(t2[i],dec(2,mul(t1[i],t2[i])));
		NTT(t2,k<<1,1);
		for(int i=k;i<(k<<1);++i)t2[i]=0;
	}
	memcpy(b,t2,sizeof(int)*(n+1));
}
} //namespace NTT

struct poly:public vector<int>{
	inline int time()const{return size()-1;}
	inline poly(int tim=0,int c=0){
		resize(tim+1);
		if(tim>=0)at(0)=c;
	}
	poly(vector<int>::iterator a,vector<int>::iterator b) {
		resize(b - a + 1);
		for(int i = 0;i < size();i++) at(i) = *(a + i) ;
	}
	inline poly operator%(const int& n)const{
		poly r(*this);
		r.resize(n);
		return r;
	}
	inline poly operator%=(const int& n){
		resize(n);
		return *this;
	}
	inline poly operator+(const poly& p)const{
		int n=time(),m=p.time();
		poly r(*this);
		if(n<m)r.resize(m+1);
		for(int i=0;i<=m;++i)r[i]=inc(r[i],p[i]);
		return r;
	}
	inline poly operator-(const poly& p)const{
		int n=time(),m=p.time();
		poly r(*this);
		if(n<m)r.resize(m+1);
		for(int i=0;i<=m;++i)r[i]=dec(r[i],p[i]);
		return r;
	}
	inline poly operator*(const poly& p)const{
		poly r(time()+p.time());
		NTT::multi(&((*this)[0]),&p[0],&r[0],time(),p.time());
		return r;
	}
	inline poly operator*(const int& k)const{
		poly r(*this);
		int n=time();
		for(int i=0;i<=n;++i)r[i]=mul(r[i],k);
		return r;
	}
	inline poly operator~()const{
		poly r(*this);
		reverse(r.begin(),r.end());
		return r;
	}
};

inline poly inv(const poly& a){
	poly r(a.time());
	NTT::inver(&a[0],&r[0],a.time());
	return r;
}
inline poly der(const poly& a){
	int n=a.time();
	poly r(n-1);
	for(int i=1;i<=n;++i)r[i-1]=mul(a[i],i);
	return r;
}
int _[N];
inline poly itr(const poly& a){
	int n=a.time();
	poly r(n+1);
	_[1]=1;
	for(int i=2;i<=n+1;++i)_[i]=mul(_[mod%i],mod-mod/i);
	for(int i=0;i<=n;++i)r[i+1]=mul(a[i],_[i+1]);
	return r;
}
inline poly ln(const poly& a){
	return itr(der(a)*inv(a)%a.time());
}
inline poly exp(const poly& a){
	poly r(0,1);
	int n=a.time(),k=1;
	while(r.time()<n)
		r%=k,r=r*(a%k-ln(r)+poly(0,1))%k,k<<=1;
	return r%(n+1);
}
inline poly qpow(const poly& a,int k){
	return exp(ln(a)*k);
}

inline poly operator/(const poly& f,const poly& g){
	int n=f.time()-g.time()+1;
	return ~((~f)*inv((~g)%n)%n);
}
inline poly operator%(const poly& f,const poly& g){
	if(f.time()<g.time())return f;
	return (f-g*(f/g)%g.time())%g.time();
}

int t[250005] , rt[250005] , iv[250005];
poly f , g;

void solve(int l,int r) {
	// printf("ON %d %d\n" , l , r);
	if(l == r) {
		if(l) f[l] = 1LL * f[l] * iv[l] % mod ;
		return ; 
	}
	int md = (l + r >> 1);
	solve(l , md) ;
	if(l == 0) {
		poly a(f.begin() , f.begin() + md) ;
		poly b = a * a;
		b.resize(r) ;
		poly c = b * a;
		for(int i = md + 1 ; i <= r;i++) {
			f[i] = ((ll)f[i] + b[i - 1] + c[i - 1]) %mod;
		}
	}
	else {
		poly a(f.begin() + l , f.begin() + md) ;
		poly b(f.begin() , f.begin() + r - l + 1) ;
		poly d = a * b; d.resize(r - l);
		poly c = d * b; 
		assert(r - l + 1 <= md) ;
		// printf("AS %d\n",a.size()) ;
		// printf("A %d %d : %d\n",l,r,md);
		for(int i = md + 1 ; i <= r ;i++) {
			// printf("%d %d %d\n",i,1LL * f[i] * t[i - 1] % mod, 1LL * c[i-1 - l] * t[i - 1] % mod) ;
			f[i] = (f[i] + 3LL * c[i - 1 - l] + 2LL * d[i - l - 1]) % mod; 
		}
	}
	solve(md + 1 , r);
}
void solve2(int l,int r) {
	if(l == r) {
		if(l) g[l] = 1LL * g[l] * iv[l] % mod;
		return ;
	}
	int md = (l + r >> 1);
	solve2(l , md) ;
	if(l == 0) {
		poly a(g.begin() , g.begin() + md) ;
		poly b(f.begin() , f.begin() + md) ;
		b = a * b;
		b.resize(r);
		b = b * a;
		for(int i = md + 1;i <= r;i++) {
			g[i] = (g[i] + b[i - 1]) % mod;
		}
	}
	else {
		poly a(g.begin() , g.begin() + r - l + 1);
		poly b(f.begin() , f.begin() + r - l + 1);
		poly c(g.begin() + l , g.begin() + md) ;
		poly d(f.begin() + l , f.begin() + md);
		// printf("IN %d %d\n",l,r);
		poly e = c * b* 2 + d * a;
		e.resize(r - l) ;
		e = e * a;
		for(int i = md + 1;i <= r;i++) {
			g[i] = (g[i] + e[i - l - 1]) % mod;
		}
	}
	solve2(md + 1 , r);
}
int main() {
    int n ; cin >> n;
    t[0] =1 ; rt[0] = 1;
    for(int i = 1;i <= n + 2;i++) {
		t[i] = 1LL * t[i - 1] * i % mod ;
		if(i == 1) iv[i] = 1;
		else iv[i] = 1LL * (mod - mod / i) * iv[mod % i] % mod ;
		 rt[i] = 1LL * rt[i - 1] * iv[i] % mod;
	}
	f.resize(n + 1); f[0] = 1;
	g.resize(n + 1) ; g[0] = 1;
	solve(0 , n);
	solve2(0 , n);
	cout << 1LL*g[n]*t[n] % mod << '\n';
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3540kb

input:

3

output:

28

result:

ok 1 number(s): "28"

Test #2:

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

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

2

output:

4

result:

ok 1 number(s): "4"

Test #4:

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

input:

4

output:

282

result:

ok 1 number(s): "282"

Test #5:

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

input:

5

output:

3718

result:

ok 1 number(s): "3718"

Test #6:

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

input:

6

output:

60694

result:

ok 1 number(s): "60694"

Test #7:

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

input:

7

output:

1182522

result:

ok 1 number(s): "1182522"

Test #8:

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

input:

8

output:

26791738

result:

ok 1 number(s): "26791738"

Test #9:

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

input:

9

output:

692310518

result:

ok 1 number(s): "692310518"

Test #10:

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

input:

10

output:

135061370

result:

ok 1 number(s): "135061370"

Test #11:

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

input:

100

output:

423669705

result:

ok 1 number(s): "423669705"

Test #12:

score: 0
Accepted
time: 5ms
memory: 3648kb

input:

1234

output:

878522960

result:

ok 1 number(s): "878522960"

Test #13:

score: 0
Accepted
time: 419ms
memory: 7584kb

input:

54321

output:

827950477

result:

ok 1 number(s): "827950477"

Test #14:

score: 0
Accepted
time: 474ms
memory: 8316kb

input:

65536

output:

380835743

result:

ok 1 number(s): "380835743"

Test #15:

score: 0
Accepted
time: 1013ms
memory: 13232kb

input:

131072

output:

842796122

result:

ok 1 number(s): "842796122"

Test #16:

score: 0
Accepted
time: 951ms
memory: 13180kb

input:

131071

output:

981021531

result:

ok 1 number(s): "981021531"

Test #17:

score: 0
Accepted
time: 952ms
memory: 13192kb

input:

131070

output:

480197639

result:

ok 1 number(s): "480197639"

Test #18:

score: 0
Accepted
time: 1067ms
memory: 14300kb

input:

131074

output:

383000585

result:

ok 1 number(s): "383000585"

Test #19:

score: 0
Accepted
time: 1050ms
memory: 13228kb

input:

131073

output:

316664839

result:

ok 1 number(s): "316664839"

Test #20:

score: -100
Time Limit Exceeded

input:

250000

output:


result: