QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#431914#8721. 括号序列A_zjzjAC ✓1517ms24456kbC++144.9kb2024-06-06 11:55:182024-06-06 11:55:20

Judging History

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

  • [2024-06-06 11:55:20]
  • 评测
  • 测评结果:AC
  • 用时:1517ms
  • 内存:24456kb
  • [2024-06-06 11:55:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define all(a) (a).begin(),(a).end()
#ifdef DEBUG
template<class T>
ostream& operator << (ostream &out,vector<T> a){
	out<<'[';
	for(T x:a)out<<x<<',';
	return out<<']';
}
template<class T>
vector<T> ary(T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<class T>
void debug(T x){
	cerr<<x<<endl;
}
template<class T,class...S>
void debug(T x,S...y){
	cerr<<x<<' ',debug(y...);
}
#else
#define debug(...) void()
#endif
const int N=2.5e5+10,mod=998244353;
ll qpow(ll x,ll y=mod-2,ll ans=1){
	for(;y;(x*=x)%=mod,y>>=1)if(y&1)(ans*=x)%=mod;
	return ans;
}
namespace Poly{
	const int N=1<<19,g=3;
	int lim,rev[N],pw[N];
	void Init(){
		int base=qpow(g,(mod-1)/N);
		pw[N/2]=1;
		for(int i=N/2+1;i<N;i++)pw[i]=1ll*pw[i-1]*base%mod;
		for(int i=N/2-1;i>=1;i--)pw[i]=pw[i<<1];
	}
	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 NTT(int *a,int op){
		for(int i=0;i<lim;i++)if(rev[i]<i)swap(a[rev[i]],a[i]);
		for(int len=1;len<lim;len<<=1){
			for(int i=0;i<lim;i+=len<<1){
				for(int j=0;j<len;j++){
					int x=1ll*a[i|j|len]*pw[len|j]%mod;
					a[i|j|len]=(a[i|j]+mod-x)%mod;
					a[i|j]=(a[i|j]+x)%mod;
				}
			}
		}
		if(op<0){
			reverse(a+1,a+lim);
			int x=qpow(lim);
			for(int i=0;i<lim;i++)a[i]=1ll*a[i]*x%mod;
		}
	}
	namespace Public{
		using poly=vector<int>;
		poly operator * (const poly &a,const poly &b){
			static int A[N],B[N];
			int n=a.size(),m=b.size();
			init(n+m-1);
			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);
			return poly{A,A+n+m-1};
		}
		poly operator % (const poly &a,const int &n){
			poly b(a);
			if(n<b.size())b.resize(n);
			return b;
		}
		poly operator * (const poly &a,const int &k){
			poly b(a);
			for(int &x:b)x=1ll*x*k%mod;
			return b;
		}
		poly operator + (const poly &a,const poly &b){
			poly c(max(a.size(),b.size()),0);
			for(int i=0;i<a.size();i++)c[i]=a[i];
			for(int i=0;i<b.size();i++)(c[i]+=b[i])%=mod;
			return c;
		}
		poly operator - (const poly &a,const poly &b){
			poly c(max(a.size(),b.size()),0);
			for(int i=0;i<a.size();i++)c[i]=a[i];
			for(int i=0;i<b.size();i++)(c[i]+=mod-b[i])%=mod;
			return c;
		}
		poly inv(const poly &a,int k=-1){
			if(!~k)k=a.size();
			poly f(1,qpow(a[0]));
			for(int len=2;;len<<=1){
				int n=min(len,k);
				f=(f*2-a%n*f%n*f%n)%n;
				if(n>=k)break;
			}
			return f;
		}
		poly jifen(const poly &a){
			if(a.empty())return poly();
			poly b(a.size()+1,0);
			b[1]=1;
			for(int i=2;i<b.size();i++)b[i]=1ll*b[mod%i]*(mod-mod/i)%mod;
			for(int i=1;i<b.size();i++)b[i]=1ll*b[i]*a[i-1]%mod;
			return b;
		}
		poly qiudao(const poly &a){
			if(a.empty())return poly();
			poly b(a.size()-1,0);
			for(int i=1;i<a.size();i++)b[i-1]=1ll*a[i]*i%mod;
			return b;
		}
		poly ln(const poly &a,int k=-1){
			if(!~k)k=a.size();
			return jifen(inv(a)*qiudao(a)%k)%k;
		}
		poly exp(const poly &a,int k=-1){
			if(!~k)k=a.size();
			poly f(1,1);
			for(int len=2;;len<<=1){
				int n=min(len,k);
				f=(poly({1})-ln(f,n)+a%n)*f%n;
				if(n>=k)break;
			}
			return f;
		}
		poly pow(const poly &a,ll y,int k=-1){
			if(a.empty())return poly();
			poly f(a);
			assert(f[0]);
			int iv=qpow(f[0]),his=qpow(a[0],y);
			for(int &x:f)x=1ll*x*iv%mod;
			f=ln(f,k);
			for(int &x:f)x=y%mod*x%mod;
			f=exp(f,k);
			for(int &x:f)x=1ll*x*his%mod;
			return f;
		}
		poly sqrt(const poly &a,int k=-1){
			if(a.empty())return poly();
			if(!~k)k=a.size();
			poly f(1,::sqrt(a[0]));
			for(int len=2,i2=(mod+1)/2;;len<<=1){
				int n=min(len,k);
				f=(f+a%n*inv(f,n)%n)*i2;
				if(n>=k)break;
			}
			return f;
		}
		void div(const poly &a,const poly &b,poly &p,poly &q){
			int n=a.size(),m=b.size();
			poly c(a),d(b);
			reverse(all(c)),reverse(all(d));
			p=c%(n-m+1)*inv(d%(n-m+1),n-m+1)%(n-m+1);
			reverse(all(p));
			q=(a-p*b)%(m-1);
		}
	}
}
using namespace Poly::Public;
int n,f[N];
void divide(int l,int r){
	if(l==r){
		f[l]=(f[l]+f[l-1]*(l-1ll))%mod*qpow(l)%mod;
		return;
	}
	int mid=(l+r)>>1;
	divide(l,mid);
	poly f1(r-l+1),f2{f+l,f+1+mid};
	for(int i=0;i<=r-l;i++)f1[i]=1ll*i*f[i]%mod;
	f1=f1*f2;
	for(int i=mid+1;i<=r;i++)(f[i]+=f1[i-l])%=mod;
	f1=poly{f,f+r-l+1},f2.assign(mid-l+1,0);
	for(int i=l;i<=mid;i++)f2[i-l]=1ll*i*f[i]%mod;
	f1=f1*f2;
	for(int i=mid+1;i<=r;i++)(f[i]+=f1[i-l])%=mod;
	divide(mid+1,r);
}
void solve(int n){
	if(n==1)return;
	int m=n>>1;
	solve(m);
	poly f1{f,f+1+m},f2(m+1);
	for(int i=0;i<=m;i++)f2[i]=1ll*i*f[i]%mod;
	f1=f1*f2;
	for(int i=m+1;i<=n;i++)(f[i]+=f1[i])%=mod;
	divide(m+1,n);
}
int main(){
	Poly::Init();
	cin>>n,f[1]=1;
	solve(n);
	auto res=inv(poly({1})-poly{f,f+1+n});
	int ans=res[n];
	for(int i=1;i<=n;i++)ans=1ll*ans*i%mod;
	cout<<ans<<endl;
	return 0;
}

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

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 11876kb

input:

3

output:

28

result:

ok 1 number(s): "28"

Test #2:

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

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

2

output:

4

result:

ok 1 number(s): "4"

Test #4:

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

input:

4

output:

282

result:

ok 1 number(s): "282"

Test #5:

score: 0
Accepted
time: 3ms
memory: 10532kb

input:

5

output:

3718

result:

ok 1 number(s): "3718"

Test #6:

score: 0
Accepted
time: 2ms
memory: 9772kb

input:

6

output:

60694

result:

ok 1 number(s): "60694"

Test #7:

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

input:

7

output:

1182522

result:

ok 1 number(s): "1182522"

Test #8:

score: 0
Accepted
time: 3ms
memory: 11752kb

input:

8

output:

26791738

result:

ok 1 number(s): "26791738"

Test #9:

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

input:

9

output:

692310518

result:

ok 1 number(s): "692310518"

Test #10:

score: 0
Accepted
time: 3ms
memory: 9556kb

input:

10

output:

135061370

result:

ok 1 number(s): "135061370"

Test #11:

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

input:

100

output:

423669705

result:

ok 1 number(s): "423669705"

Test #12:

score: 0
Accepted
time: 6ms
memory: 9976kb

input:

1234

output:

878522960

result:

ok 1 number(s): "878522960"

Test #13:

score: 0
Accepted
time: 296ms
memory: 14176kb

input:

54321

output:

827950477

result:

ok 1 number(s): "827950477"

Test #14:

score: 0
Accepted
time: 349ms
memory: 14072kb

input:

65536

output:

380835743

result:

ok 1 number(s): "380835743"

Test #15:

score: 0
Accepted
time: 756ms
memory: 19900kb

input:

131072

output:

842796122

result:

ok 1 number(s): "842796122"

Test #16:

score: 0
Accepted
time: 668ms
memory: 18224kb

input:

131071

output:

981021531

result:

ok 1 number(s): "981021531"

Test #17:

score: 0
Accepted
time: 695ms
memory: 18004kb

input:

131070

output:

480197639

result:

ok 1 number(s): "480197639"

Test #18:

score: 0
Accepted
time: 835ms
memory: 19976kb

input:

131074

output:

383000585

result:

ok 1 number(s): "383000585"

Test #19:

score: 0
Accepted
time: 847ms
memory: 19988kb

input:

131073

output:

316664839

result:

ok 1 number(s): "316664839"

Test #20:

score: 0
Accepted
time: 1517ms
memory: 24436kb

input:

250000

output:

119658643

result:

ok 1 number(s): "119658643"

Test #21:

score: 0
Accepted
time: 1480ms
memory: 24360kb

input:

249999

output:

78110138

result:

ok 1 number(s): "78110138"

Test #22:

score: 0
Accepted
time: 1456ms
memory: 24456kb

input:

249998

output:

297253469

result:

ok 1 number(s): "297253469"

Extra Test:

score: 0
Extra Test Passed