QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417166 | #8721. 括号序列 | ucup_team_qiuly# | AC ✓ | 1641ms | 26628kb | C++17 | 3.6kb | 2024-05-22 15:37:18 | 2024-05-22 15:37:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+5,MOD=998244353;
int n,ans,a[N],b[N],f[N],g[N];
int l,lim,lim1,invL,r[N],inv[N],tmp1[N],tmp2[N],tmp3[N],g1[2][N];
void W(int &x,int y) {x+=y;if(x>=MOD) x-=MOD;}
int add(int x,int y) {x+=y;return x<MOD?x:x-MOD;}
int qPow(int x,int y)
{int res=1;for(;y;y/=2,x=1ll*x*x%MOD) if(y&1) res=1ll*res*x%MOD;return res;}
void init(int n)
{
l=0;lim=1;while(lim<n) ++l,lim*=2;invL=qPow(lim,MOD-2);
for(int i=0;i<lim;++i) r[i]=(r[i>>1]>>1)|((i&1)<<l-1);
if(lim>lim1)
{
for(int i=1;i<lim;++i) inv[i]=i>1?1ll*inv[MOD%i]*(MOD-MOD/i)%MOD:1;
for(int i=1,t1,t2,t3,t4;i<lim;i*=2)
{
t1=qPow(3,(MOD-1)/(i*2));t2=qPow(t1,MOD-2);t3=t4=1;
for(int j=0;j<i;++j,t3=1ll*t3*t1%MOD,t4=1ll*t4*t2%MOD)
g1[0][i+j]=t3,g1[1][i+j]=t4;
}lim1=lim;
}
}
void deriv(int n,int a[]) {for(int i=1;i<n;++i) a[i-1]=1ll*a[i]*i%MOD;a[n-1]=0;}
void integ(int n,int a[]) {for(int i=n-1;i;--i) a[i]=1ll*a[i-1]*inv[i]%MOD;a[0]=0;}
void NTT(bool fl,int a[])
{
for(int i=0;i<lim;++i) if(i<r[i]) swap(a[i],a[r[i]]);
for(int i=1,t1,t2;i<lim;i*=2) for(int j=0;j<lim;j+=i*2) for(int k=0;k<i;++k)
{
t1=a[j+k];t2=1ll*g1[fl][i+k]*a[i+j+k]%MOD;
a[j+k]=add(t1,t2);a[i+j+k]=add(t1,MOD-t2);
}if(fl) for(int i=0;i<lim;++i) a[i]=1ll*a[i]*invL%MOD;
}
void polyInv(int n,int a[],int res[])
{
if(n==1) {res[0]=qPow(a[0],MOD-2);return;}polyInv((n+1)/2,a,res);
for(int i=0;i<n;++i) tmp1[i]=a[i];for(int i=n;i<lim;++i) tmp1[i]=0;
init(n*2-1);NTT(0,tmp1);NTT(0,res);
for(int i=0;i<lim;++i) res[i]=(2-1ll*tmp1[i]*res[i]%MOD+MOD)*res[i]%MOD;
NTT(1,res);for(int i=n;i<lim;++i) res[i]=0;
}
void polyLn(int n,int a[])
{
init(n*2-1);for(int i=0;i<lim;++i) tmp2[i]=0;
polyInv(n,a,tmp2);deriv(n,a);NTT(0,a);NTT(0,tmp2);
for(int i=0;i<lim;++i) a[i]=1ll*a[i]*tmp2[i]%MOD;
NTT(1,a);integ(n,a);for(int i=n;i<lim;++i) a[i]=0;
}
void polyExp(int n,int a[],int res[])
{
if(n==1) {res[0]=1;return;}polyExp((n+1)/2,a,res);
for(int i=0;i<n;++i) tmp3[i]=res[i];for(int i=n;i<lim;++i) tmp3[i]=0;
polyLn(n,tmp3);for(int i=0;i<n;++i) tmp3[i]=add(a[i],MOD-tmp3[i]);++tmp3[0];
NTT(0,tmp3);NTT(0,res);for(int i=0;i<lim;++i) res[i]=1ll*res[i]*tmp3[i]%MOD;
NTT(1,res);for(int i=n;i<lim;++i) res[i]=0;
}
void slv(int l,int r)
{
if(l==r) {W(g[l],f[l]);return;}
int mid=(l+r)/2,t;slv(l,mid);
t=min(mid,r-l);init(mid-l+t+1);
fill(a,a+lim,0);fill(b,b+lim,0);
copy(f+l,f+mid+1,a);
for(int i=1;i<=t;++i) b[i]=1ll*g[i]*inv[i]%MOD;
NTT(0,a);NTT(0,b);
for(int i=0;i<lim;++i) a[i]=1ll*a[i]*b[i]%MOD;NTT(1,a);
for(int i=mid+1;i<=r && i<=mid+t;++i) W(f[i],a[i-l]);
t=min(l-1,r-l);init(mid-l+t+1);
fill(a,a+lim,0);fill(b,b+lim,0);
copy(f+1,f+t+1,a+1);
for(int i=l;i<=mid;++i) b[i-l]=1ll*g[i]*inv[i]%MOD;
NTT(0,a);NTT(0,b);
for(int i=0;i<lim;++i) a[i]=1ll*a[i]*b[i]%MOD;NTT(1,a);
for(int i=mid+1;i<=r && i<=mid+t;++i) W(f[i],a[i-l]);
t=min(mid,r-l);init(mid-l+t+1);
fill(a,a+lim,0);fill(b,b+lim,0);
copy(f+l,f+mid+1,a);copy(g+1,g+t+1,b+1);
NTT(0,a);NTT(0,b);
for(int i=0;i<lim;++i) a[i]=1ll*a[i]*b[i]%MOD;NTT(1,a);
for(int i=mid+1;i<=r && i<=mid+t;++i) W(g[i],a[i-l]);
t=min(l-1,r-l);init(mid-l+t+1);
fill(a,a+lim,0);fill(b,b+lim,0);
copy(f+1,f+t+1,a+1);copy(g+l,g+mid+1,b);
NTT(0,a);NTT(0,b);
for(int i=0;i<lim;++i) a[i]=1ll*a[i]*b[i]%MOD;NTT(1,a);
for(int i=mid+1;i<=r && i<=mid+t;++i) W(g[i],a[i-l]);
slv(mid+1,r);
}
int main()
{
scanf("%d",&n);++n;f[1]=g[0]=1;init(n);slv(1,n);
ans=f[n];for(int i=1;i<n;++i) ans=1ll*ans*i%MOD;
printf("%d\n",ans);return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 16216kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 0ms
memory: 16224kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 3ms
memory: 16088kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 16236kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 0ms
memory: 16228kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 2ms
memory: 16168kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 0ms
memory: 16136kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 0ms
memory: 16172kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 0ms
memory: 16168kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 0ms
memory: 16220kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 3ms
memory: 16276kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 3ms
memory: 16288kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 325ms
memory: 16604kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 364ms
memory: 23220kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: 0
Accepted
time: 803ms
memory: 24232kb
input:
131072
output:
842796122
result:
ok 1 number(s): "842796122"
Test #16:
score: 0
Accepted
time: 747ms
memory: 17196kb
input:
131071
output:
981021531
result:
ok 1 number(s): "981021531"
Test #17:
score: 0
Accepted
time: 747ms
memory: 21168kb
input:
131070
output:
480197639
result:
ok 1 number(s): "480197639"
Test #18:
score: 0
Accepted
time: 807ms
memory: 23628kb
input:
131074
output:
383000585
result:
ok 1 number(s): "383000585"
Test #19:
score: 0
Accepted
time: 809ms
memory: 22212kb
input:
131073
output:
316664839
result:
ok 1 number(s): "316664839"
Test #20:
score: 0
Accepted
time: 1641ms
memory: 22144kb
input:
250000
output:
119658643
result:
ok 1 number(s): "119658643"
Test #21:
score: 0
Accepted
time: 1629ms
memory: 24640kb
input:
249999
output:
78110138
result:
ok 1 number(s): "78110138"
Test #22:
score: 0
Accepted
time: 1640ms
memory: 26628kb
input:
249998
output:
297253469
result:
ok 1 number(s): "297253469"
Extra Test:
score: 0
Extra Test Passed