QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#417986#8721. 括号序列grass8cowTL 1402ms28968kbC++172.5kb2024-05-23 08:37:322024-05-23 08:37:32

Judging History

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

  • [2024-05-23 08:37:32]
  • 评测
  • 测评结果:TL
  • 用时:1402ms
  • 内存:28968kb
  • [2024-05-23 08:37:32]
  • 提交

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],jc[251000],ij[250100],n,g[250100],inv[250100];
int C(int a,int b){
    return 1ll*jc[a]*ij[b]%mod*ij[a-b]%mod;
}
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)%mod,a[i+j+o]=(x-y)%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 cdq(int l,int r){
    if(l==r){if(!l)return;(f[l]+=1ll*g[l]*inv[l]%mod)%=mod;return;}
    int mi=(l+r)>>1;
    cdq(l,mi);init(r-l+mi-l+3);
    //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++)(g[i+1]+=p[i-l])%=mod;
    //---
    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++)(g[i+1]+=p[i-l])%=mod;
    //---
    //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]=1ll*inv[i]*g[i]%mod;
    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++)(f[i]+=p[i-l])%=mod;
    //---
    for(int i=0;i<L;i++)p[i]=q[i]=0;
    for(int i=max(l,1);i<=mi;i++)p[i-l]=1ll*inv[i]*g[i]%mod;
    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++)(f[i]+=p[i-l])%=mod;
    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;
    }
    jc[0]=ij[0]=1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)jc[i]=1ll*i*jc[i-1]%mod,ij[i]=qpow(jc[i],mod-2),
    inv[i]=1ll*jc[i-1]*ij[i]%mod;
    f[0]=g[0]=1;
    cdq(0,n);
    return printf("%d",(1ll*f[n]*jc[n]%mod+mod)%mod),0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 12ms
memory: 20188kb

input:

3

output:

28

result:

ok 1 number(s): "28"

Test #2:

score: 0
Accepted
time: 4ms
memory: 22304kb

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #3:

score: 0
Accepted
time: 13ms
memory: 22352kb

input:

2

output:

4

result:

ok 1 number(s): "4"

Test #4:

score: 0
Accepted
time: 4ms
memory: 22364kb

input:

4

output:

282

result:

ok 1 number(s): "282"

Test #5:

score: 0
Accepted
time: 12ms
memory: 20184kb

input:

5

output:

3718

result:

ok 1 number(s): "3718"

Test #6:

score: 0
Accepted
time: 8ms
memory: 22412kb

input:

6

output:

60694

result:

ok 1 number(s): "60694"

Test #7:

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

input:

7

output:

1182522

result:

ok 1 number(s): "1182522"

Test #8:

score: 0
Accepted
time: 13ms
memory: 22228kb

input:

8

output:

26791738

result:

ok 1 number(s): "26791738"

Test #9:

score: 0
Accepted
time: 12ms
memory: 22364kb

input:

9

output:

692310518

result:

ok 1 number(s): "692310518"

Test #10:

score: 0
Accepted
time: 8ms
memory: 22400kb

input:

10

output:

135061370

result:

ok 1 number(s): "135061370"

Test #11:

score: 0
Accepted
time: 13ms
memory: 20328kb

input:

100

output:

423669705

result:

ok 1 number(s): "423669705"

Test #12:

score: 0
Accepted
time: 14ms
memory: 22412kb

input:

1234

output:

878522960

result:

ok 1 number(s): "878522960"

Test #13:

score: 0
Accepted
time: 491ms
memory: 28804kb

input:

54321

output:

827950477

result:

ok 1 number(s): "827950477"

Test #14:

score: 0
Accepted
time: 528ms
memory: 21080kb

input:

65536

output:

380835743

result:

ok 1 number(s): "380835743"

Test #15:

score: 0
Accepted
time: 1402ms
memory: 28940kb

input:

131072

output:

842796122

result:

ok 1 number(s): "842796122"

Test #16:

score: 0
Accepted
time: 1103ms
memory: 28308kb

input:

131071

output:

981021531

result:

ok 1 number(s): "981021531"

Test #17:

score: 0
Accepted
time: 1137ms
memory: 21764kb

input:

131070

output:

480197639

result:

ok 1 number(s): "480197639"

Test #18:

score: 0
Accepted
time: 1116ms
memory: 21900kb

input:

131074

output:

383000585

result:

ok 1 number(s): "383000585"

Test #19:

score: 0
Accepted
time: 1164ms
memory: 28968kb

input:

131073

output:

316664839

result:

ok 1 number(s): "316664839"

Test #20:

score: -100
Time Limit Exceeded

input:

250000

output:


result: