QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#426359#8721. 括号序列forget-starWA 2ms26164kbC++204.2kb2024-05-31 08:47:022024-05-31 08:47:04

Judging History

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

  • [2024-05-31 08:47:04]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:26164kb
  • [2024-05-31 08:47:02]
  • 提交

answer

#include<bits/stdc++.h>
#define de fprintf(stderr,"on-%d\n",__LINE__)
//#define int long long
#define fr(i,a,b) for(int i(a),end##i(b);i<=end##i;i++)
#define fd(i,a,b) for(int i(a),end##i(b);i>=end##i;i--)
#define vec vector<int>
using namespace std;
const int maxn=2e6+5,mod=998244353;
inline int upd(const int &x){
	if(x<0)return x+mod;
	if(x>=mod)return x-mod;
	return x;
}
inline void add(int &x,const int &y){
	x=upd(x+y);
}
int ksm(int x,int k){
	int ans=1;
	while(k){
		if(k&1)ans=1ll*ans*x%mod;
		x=1ll*x*x%mod;
		k>>=1;
	}
	return ans;
}
int jc[maxn],iv[maxn];
void init(int n){
	jc[0]=1;
	fr(i,1,n)jc[i]=1ll*jc[i-1]*i%mod;
	iv[n]=ksm(jc[n],mod-2);
	fd(i,n-1,0)iv[i]=1ll*iv[i+1]*(i+1)%mod;
}
int ny(int n){
	return 1ll*iv[n]*jc[n-1]%mod;
}
int rev[maxn];
void INIT(int n){
	fr(i,1,n-1)rev[i]=(rev[i>>1]>>1)|(i&1?n>>1:0);
}
const int invg=ksm(3,mod-2);
void F(int *f,int n,int h){
const int G=3;
	fr(i,0,n-1)if(rev[i]<i)swap(f[rev[i]],f[i]);
	for(int p=1;p<n;p<<=1){
		int wn=ksm(h==1?G:invg,(mod-1)/(p<<1));	
		for(int i=0;i<n;i+=p<<1)
			for(int k=i,w=1;k<i+p;k++,w=1ll*w*wn%mod){
				int x=f[k],y=1ll*f[k+p]*w%mod;	
				f[k]=upd(x+y);
				f[k+p]=upd(x-y);
			}
	}
	if(h==-1){
		int invn=ny(n);
		fr(i,0,n-1)f[i]=1ll*f[i]*invn%mod;
	}
}
vector<int> operator *(const vector<int>& A,const vector<int>& B){
	static int a[maxn],b[maxn]; 
	int N=1;
	while(N-1<=A.size()-1+B.size()-1)N<<=1;
	fr(i,1,N)rev[i]=(rev[i>>1]>>1)|(i&1?N>>1:0);
	fr(i,0,N-1)a[i]=b[i]=0;
	fr(i,0,A.size()-1)a[i]=A[i];fr(i,0,B.size()-1)b[i]=B[i];
	F(a,N,1);F(b,N,1);
	fr(i,0,N-1)a[i]=1ll*a[i]*b[i]%mod;
	F(a,N,-1);
	vector<int> C;C.resize(A.size()+B.size()-1);
	fr(i,0,C.size()-1)C[i]=a[i];
	return C;
}
vector<int> operator -(vector<int> A,const vector<int>&B){
	if(B.size()>A.size())A.resize(B.size());
	fr(i,0,B.size()-1)add(A[i],-B[i]);
	return A;
}
vector<int> operator +(vector<int> A,const vector<int>&B){
	if(B.size()>A.size())A.resize(B.size());
	fr(i,0,B.size()-1)add(A[i],B[i]);
	return A;
}
vector<int> operator *(vector<int> A,const int &x){
	fr(i,0,A.size()-1)A[i]=1ll*A[i]*x%mod;
	return A;
}

void out(vec A){
	fr(i,0,A.size()-1)cout<<A[i]<<' ';cout<<'\n';
}
int lg[maxn];
int g[maxn],h[maxn];
int G[20][maxn];
int H[20][maxn];
int A[maxn],B[maxn],D[maxn],E[maxn],P[maxn],Q[maxn];
void solve(int l,int r){
	if(l==r){
		add(h[l],1ll*g[l]*(l+2)%mod);
		add(g[l+1],1ll*(g[l]+h[l])*ny(l+1)%mod);
		return ;
	}
	int n=r-l+1,t=lg[n];
	solve(l,l+n/2-1);
	fr(i,0,n/2-1)A[i]=g[i+l],B[i]=h[i+l];
	fr(i,n/2,n-1)A[i]=B[i]=0;
	//fr(i,1,n-1)rev[i]=(rev[i>>1]>>1)|(i&1?n>>1:0);
	INIT(n);
	F(A,n,1);
	F(B,n,1);
	fr(i,0,n-1){
		D[i]=(1ll*B[i]*G[t][i]+1ll*A[i]*H[t][i])%mod;
		E[i]=2ll*A[i]*G[t][i]%mod;
	}
	F(D,n,-1);
	F(E,n,-1);
	fr(i,n/2,n-1){
		add(g[l+i+1],1ll*D[i]*ny(l+i+1)%mod);
		add(h[l+i],E[i]);
	}
	solve(l+n/2,r);
}
void Solve(int N){
	lg[1]=0;
	fr(i,2,N/2-1)lg[i]=lg[i>>1]+1;
	g[0]=h[0]=1;g[1]=1;
	//G[0][0]=H[0][0]=1;
	for(int n=2;n<=N;n<<=1){
	//	cout<<g[n/2]<<'\n';
		//add(h[n/2],1ll*g[n/2]*(n/2+2)%mod);
	//	add(g[n/2+1],1ll*(g[n/2]+h[n/2])*ny(n/2+1)%mod);
		solve(n/2,n-1);
		//fr(i,0,n-1)cout<<g[i]<<' ';cout<<'\n';
	//	fr(i,0,n-1)cout<<h[i]<<' ';cout<<'\n';
		if(n==N)break;
		int t=lg[n];
		fr(i,0,n-1){
			G[t][i]=g[i];
			H[t][i]=h[i];
		}
		INIT(n);
		F(G[t],n,1);
		F(H[t],n,1);
		INIT(n<<1);
		fr(i,0,n-1){
			A[i]=g[i];
			B[i]=h[i];
		}
		fr(i,n,2*n-1)A[i]=B[i]=0;
		//fr(i,n/2,n-1)A[i]=B[i]=0;
		F(A,2*n,1);F(B,2*n,1);
		fr(i,0,2*n-1){
			D[i]=1ll*A[i]*B[i]%mod;
			E[i]=1ll*A[i]*A[i]%mod;
		}
		F(D,2*n,-1);F(E,2*n,-1);
		fr(i,n,2*n-1){	
			add(g[i+1],1ll*D[i]*ny(i+1)%mod);
			add(h[i],E[i]);
		}
	}
}
void Sv(int n){
	static int h[100],g[100];
	h[0]=g[0]=1;
	fr(i,1,n){
		fr(j,0,i-1)add(g[i],1ll*h[j]%mod*g[i-1-j]%mod*ny(i)%mod);
		fr(j,0,i)add(h[i],1ll*g[j]*g[i-j]%mod);
		add(h[i],1ll*i*g[i]%mod);
	}
	fr(i,0,n-1)cout<<g[i]<<' ';cout<<'\n';
}
signed main(){
#ifdef pig
	freopen("pig.in","r",stdin);
	freopen("pig.out","w",stdout);
#endif
	int n;
	vector<int> A;
	cin>>n;	
	int N=1;
	while(N<n+1)N<<=1;
	init(N<<1);
	Solve(N);
	cout<<N<<'\n';
	//Sv(N);
	cout<<1ll*g[n]*jc[n]%mod;
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 26164kb

input:

3

output:

4
28

result:

wrong answer 1st numbers differ - expected: '28', found: '4'