QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#426357 | #8721. 括号序列 | forget-star | WA | 1203ms | 100892kb | C++20 | 4.2kb | 2024-05-31 08:43:10 | 2024-05-31 08:43:11 |
Judging History
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=1e6+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<<2);
Solve(N);
//Sv(N);
cout<<1ll*g[n]*jc[n]%mod;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 26020kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 2ms
memory: 13756kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 26140kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 30244kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 0ms
memory: 30396kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 0ms
memory: 30236kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 0ms
memory: 30228kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 0ms
memory: 34336kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 0ms
memory: 34376kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 0ms
memory: 34340kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 3ms
memory: 46600kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 7ms
memory: 62964kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 252ms
memory: 89948kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 551ms
memory: 94232kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: -100
Wrong Answer
time: 1203ms
memory: 100892kb
input:
131072
output:
510084197
result:
wrong answer 1st numbers differ - expected: '842796122', found: '510084197'