QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#418010#8721. 括号序列grass8cowAC ✓1594ms32368kbC++172.4kb2024-05-23 09:11:592024-05-23 09:12:00

Judging History

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

  • [2024-05-23 09:12:00]
  • 评测
  • 测评结果:AC
  • 用时:1594ms
  • 内存:32368kb
  • [2024-05-23 09:11:59]
  • 提交

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],q1[1<<20],q2[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]=q1[i]=q2[i]=0;
    for(int i=l;i<=mi;i++)q1[i-l]=f[i];
    for(int i=0;i<l&&i<=r-l;i++)q2[i]=f[i];
    NTT(q1,1),NTT(q2,1);
    for(int i=0;i<=mi&&i<=r-l-1;i++)p[i]=g[i];
    NTT(p,1);for(int i=0;i<L;i++)p[i]=1ll*p[i]*q1[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]=0;
    for(int i=l;i<=mi;i++)p[i-l]=g[i];
    NTT(p,1);for(int i=0;i<L;i++)p[i]=1ll*p[i]*q2[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]=0;
    for(int i=1;i<=mi&&i<=r-l;i++)p[i]=g_[i];
    NTT(p,1);for(int i=0;i<L;i++)p[i]=1ll*p[i]*q1[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]=0;
    for(int i=max(l,1);i<=mi;i++)p[i-l]=g_[i];
    NTT(p,1);for(int i=0;i<L;i++)p[i]=1ll*p[i]*q2[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;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 9ms
memory: 22224kb

input:

3

output:

28

result:

ok 1 number(s): "28"

Test #2:

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

input:

1

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

2

output:

4

result:

ok 1 number(s): "4"

Test #4:

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

input:

4

output:

282

result:

ok 1 number(s): "282"

Test #5:

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

input:

5

output:

3718

result:

ok 1 number(s): "3718"

Test #6:

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

input:

6

output:

60694

result:

ok 1 number(s): "60694"

Test #7:

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

input:

7

output:

1182522

result:

ok 1 number(s): "1182522"

Test #8:

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

input:

8

output:

26791738

result:

ok 1 number(s): "26791738"

Test #9:

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

input:

9

output:

692310518

result:

ok 1 number(s): "692310518"

Test #10:

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

input:

10

output:

135061370

result:

ok 1 number(s): "135061370"

Test #11:

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

input:

100

output:

423669705

result:

ok 1 number(s): "423669705"

Test #12:

score: 0
Accepted
time: 16ms
memory: 22368kb

input:

1234

output:

878522960

result:

ok 1 number(s): "878522960"

Test #13:

score: 0
Accepted
time: 330ms
memory: 22956kb

input:

54321

output:

827950477

result:

ok 1 number(s): "827950477"

Test #14:

score: 0
Accepted
time: 332ms
memory: 31148kb

input:

65536

output:

380835743

result:

ok 1 number(s): "380835743"

Test #15:

score: 0
Accepted
time: 790ms
memory: 31872kb

input:

131072

output:

842796122

result:

ok 1 number(s): "842796122"

Test #16:

score: 0
Accepted
time: 723ms
memory: 31808kb

input:

131071

output:

981021531

result:

ok 1 number(s): "981021531"

Test #17:

score: 0
Accepted
time: 716ms
memory: 23656kb

input:

131070

output:

480197639

result:

ok 1 number(s): "480197639"

Test #18:

score: 0
Accepted
time: 902ms
memory: 31700kb

input:

131074

output:

383000585

result:

ok 1 number(s): "383000585"

Test #19:

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

input:

131073

output:

316664839

result:

ok 1 number(s): "316664839"

Test #20:

score: 0
Accepted
time: 1588ms
memory: 32368kb

input:

250000

output:

119658643

result:

ok 1 number(s): "119658643"

Test #21:

score: 0
Accepted
time: 1579ms
memory: 32328kb

input:

249999

output:

78110138

result:

ok 1 number(s): "78110138"

Test #22:

score: 0
Accepted
time: 1594ms
memory: 32268kb

input:

249998

output:

297253469

result:

ok 1 number(s): "297253469"

Extra Test:

score: 0
Extra Test Passed