QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#417993 | #8721. 括号序列 | grass8cow | TL | 930ms | 27680kb | C++17 | 2.5kb | 2024-05-23 08:48:11 | 2024-05-23 08:48:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353,G=3,GI=(mod+1)/3;
int qpow(int a,int b){
int c=1;
for(;b;b>>=1){
if(b&1)c=1ll*a*c%mod;
a=1ll*a*a%mod;
}
return c;
}
int f[251000],n,g[250100],inv[250100],g_[250100];
int L,lb[1<<20];
void init(int n){
L=1;while(L<=n)L<<=1;
for(int i=1;i<L;i++)lb[i]=(lb[i>>1]>>1)|((i&1)?(L>>1):0);
}
int wi[1<<20][2];
void NTT(int *a,int fl){
for(int i=0;i<L;i++)if(i<lb[i])swap(a[i],a[lb[i]]);
for(int o=1;o<L;o<<=1){
for(int i=0;i<L;i+=(o<<1))for(int j=0;j<o;j++){
int x=a[i+j],y=1ll*a[i+j+o]*wi[j+o][fl]%mod;
a[i+j]=x+y;if(a[i+j]>=mod)a[i+j]-=mod;
a[i+j+o]=x+mod-y;if(a[i+j+o]>=mod)a[i+j+o]-=mod;
}
}
if(!fl){
int I=qpow(L,mod-2);
for(int i=0;i<L;i++)a[i]=1ll*a[i]*I%mod;
}
}
int p[1<<20],q[1<<20];
void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;}
void cdq(int l,int r){
if(l==r){if(!l)return;g_[l]=1ll*inv[l]*g[l]%mod;(f[l]+=1ll*g[l]*inv[l]%mod)%=mod;return;}
int mi=(l+r)>>1;
cdq(l,mi);init(r-l+mi-l+1);
//part1
for(int i=0;i<L;i++)p[i]=q[i]=0;
for(int i=l;i<=mi;i++)p[i-l]=f[i];
for(int i=0;i<=mi&&i<=r-l-1;i++)q[i]=g[i];
NTT(p,1),NTT(q,1);for(int i=0;i<L;i++)p[i]=1ll*p[i]*q[i]%mod;
NTT(p,0);
for(int i=mi;i<r;i++)ad(g[i+1],p[i-l]);
//---
for(int i=0;i<L;i++)p[i]=q[i]=0;
for(int i=l;i<=mi;i++)p[i-l]=g[i];
for(int i=0;i<l&&i<=r-l-1;i++)q[i]=f[i];
NTT(p,1),NTT(q,1);for(int i=0;i<L;i++)p[i]=1ll*p[i]*q[i]%mod;
NTT(p,0);
for(int i=mi;i<r;i++)ad(g[i+1],p[i-l]);
//---
//part2
for(int i=0;i<L;i++)p[i]=q[i]=0;
for(int i=max(l,1);i<=mi;i++)p[i-l]=f[i];
for(int i=1;i<=mi&&i<=r-l;i++)q[i]=g_[i];
NTT(p,1),NTT(q,1);for(int i=0;i<L;i++)p[i]=1ll*p[i]*q[i]%mod;
NTT(p,0);
for(int i=mi+1;i<=r;i++)ad(f[i],p[i-l]);
//---
for(int i=0;i<L;i++)p[i]=q[i]=0;
for(int i=max(l,1);i<=mi;i++)p[i-l]=g_[i];
for(int i=1;i<l&&i<=r-l;i++)q[i]=f[i];
NTT(p,1),NTT(q,1);for(int i=0;i<L;i++)p[i]=1ll*p[i]*q[i]%mod;
NTT(p,0);
for(int i=mi+1;i<=r;i++)ad(f[i],p[i-l]);
cdq(mi+1,r);
}
int main(){
for(int fl=0;fl<2;fl++)for(int o=1;o<(1<<20);o<<=1){
int Wn=qpow(fl?G:GI,(mod-1)/(o<<1)),w=1;
for(int i=0;i<o;i++,w=1ll*w*Wn%mod)wi[i+o][fl]=w;
}
scanf("%d",&n);
inv[1]=1;
for(int i=2;i<=n;i++)inv[i]=mod-1ll*inv[mod%i]*(mod/i)%mod;
f[0]=g[0]=1;
cdq(0,n);int an=f[n];for(int i=1;i<=n;i++)an=1ll*an*i%mod;
return printf("%d",(an+mod)%mod),0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 13ms
memory: 20260kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 8ms
memory: 20316kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 6ms
memory: 20312kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 12ms
memory: 20236kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 8ms
memory: 20320kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 12ms
memory: 20256kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 12ms
memory: 20260kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 6ms
memory: 20176kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 13ms
memory: 20356kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 6ms
memory: 20308kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 8ms
memory: 20356kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 12ms
memory: 20372kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 373ms
memory: 21040kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 381ms
memory: 26820kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: 0
Accepted
time: 838ms
memory: 27652kb
input:
131072
output:
842796122
result:
ok 1 number(s): "842796122"
Test #16:
score: 0
Accepted
time: 852ms
memory: 27432kb
input:
131071
output:
981021531
result:
ok 1 number(s): "981021531"
Test #17:
score: 0
Accepted
time: 882ms
memory: 27680kb
input:
131070
output:
480197639
result:
ok 1 number(s): "480197639"
Test #18:
score: 0
Accepted
time: 831ms
memory: 21384kb
input:
131074
output:
383000585
result:
ok 1 number(s): "383000585"
Test #19:
score: 0
Accepted
time: 930ms
memory: 27388kb
input:
131073
output:
316664839
result:
ok 1 number(s): "316664839"
Test #20:
score: -100
Time Limit Exceeded
input:
250000