QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#453287 | #8721. 括号序列 | NaCly_Fish | AC ✓ | 415ms | 34516kb | C++14 | 3.9kb | 2024-06-23 21:49:08 | 2024-06-23 21:49:08 |
Judging History
answer
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 524292
#define ll long long
#define p 998244353
using namespace std;
inline int add(const int& x,const int& y){ return x+y>=p?x+y-p:x+y; }
inline int dec(const int& x,const int& y){ return x<y?x-y+p:x-y; }
inline int power(int a,int t){
int res = 1;
while(t){
if(t&1) res = (ll)res*a%p;
a = (ll)a*a%p;
t >>= 1;
}
return res;
}
int siz;
int rev[N],rt[N],fac[N],ifac[N],inv[N];
void init(int n){
int lim = 1;
while(lim<=n) lim <<= 1,++siz;
for(int i=0;i!=lim;++i) rev[i] = (rev[i>>1]>>1)|((i&1)<<(siz-1));
int w = power(3,(p-1)>>siz);
fac[0] = fac[1] = ifac[0] = inv[1] = rt[lim>>1] = 1;
for(int i=(lim>>1)+1;i!=lim;++i) rt[i] = (ll)rt[i-1]*w%p;
for(int i=(lim>>1)-1;i;--i) rt[i] = rt[i<<1];
for(int i=2;i<=n;++i) fac[i] = (ll)fac[i-1]*i%p;
ifac[n] = power(fac[n],p-2);
for(int i=n-1;i;--i) ifac[i] = (ll)ifac[i+1]*(i+1)%p;
for(int i=2;i<=n;++i) inv[i] = (ll)fac[i-1]*ifac[i]%p;
}
inline void dft(int *f,int n){
static unsigned long long a[N];
int x,shift = siz-__builtin_ctz(n);
for(int i=0;i!=n;++i) a[rev[i]>>shift] = f[i];
for(int mid=1;mid!=n;mid<<=1)
for(int j=0;j!=n;j+=(mid<<1))
for(int k=0;k!=mid;++k){
x = a[j|k|mid]*rt[mid|k]%p;
a[j|k|mid] = a[j|k]+p-x;
a[j|k] += x;
}
for(int i=0;i!=n;++i) f[i] = a[i]%p;
}
inline void idft(int *f,int n){
reverse(f+1,f+n);
dft(f,n);
int x = p-((p-1)>>__builtin_ctz(n));
for(int i=0;i!=n;++i) f[i] = (ll)f[i]*x%p;
}
inline int getlen(int n){
return 1<<(32-__builtin_clz(n));
}
inline void inverse(const int *f,int n,int *r){
static int g[N],h[N],st[30];
memset(g,0,getlen(n<<1)<<2);
int lim = 1,top = 0;
while(n){
st[++top] = n;
n >>= 1;
}
g[0] = 1;
while(top--){
n = st[top+1];
while(lim<=(n<<1)) lim <<= 1;
memcpy(h,f,(n+1)<<2);
memset(h+n+1,0,(lim-n)<<2);
dft(g,lim),dft(h,lim);
for(int i=0;i!=lim;++i) g[i] = g[i]*(2-(ll)g[i]*h[i]%p+p)%p;
idft(g,lim);
memset(g+n+1,0,(lim-n)<<2);
}
memcpy(r,g,(n+1)<<2);
}
inline void log(const int *f,int n,int *r){
static int g[N],h[N];
inverse(f,n,g);
for(int i=0;i!=n;++i) h[i] = (ll)f[i+1]*(i+1)%p;
h[n] = 0;
int lim = getlen(n<<1);
memset(g+n+1,0,(lim-n)<<2);
memset(h+n+1,0,(lim-n)<<2);
dft(g,lim),dft(h,lim);
for(int i=0;i!=lim;++i) g[i] = (ll)g[i]*h[i]%p;
idft(g,lim);
for(int i=1;i<=n;++i) r[i] = (ll)g[i-1]*inv[i]%p;
r[0] = 0;
}
inline void exp(const int *f,int n,int *r){
static int g[N],h[N],st[30];
memset(g,0,getlen(n<<1)<<2);
int lim = 1,top = 0;
while(n){
st[++top] = n;
n >>= 1;
}
g[0] = 1;
while(top--){
n = st[top+1];
while(lim<=(n<<1)) lim <<= 1;
memcpy(h,g,(n+1)<<2);
memset(h+n+1,0,(lim-n)<<2);
log(g,n,g);
for(int i=0;i<=n;++i) g[i] = dec(f[i],g[i]);
g[0] = add(g[0],1);
dft(g,lim),dft(h,lim);
for(int i=0;i!=lim;++i) g[i] = (ll)g[i]*h[i]%p;
idft(g,lim);
memset(g+n+1,0,(lim-n)<<2);
}
memcpy(r,g,(n+1)<<2);
}
/*
x = G - 2 exp(G) + 2
ans = n![x^n](1+G)^(-1)
*/
int n;
int f[N],g[N];
int main(){
scanf("%d",&n);
init(n<<1);
f[1] = 1;
for(int i=2;i<=n+1;++i) f[i] = 2*ifac[i]%p;
for(int i=0;i<=n+1;++i) f[i] = f[i+1];
log(f,n,f);
for(int i=0;i<=n;++i) f[i] = (ll)f[i]*(p-n-1)%p;
exp(f,n,f);
for(int i=0;i<=n;++i) g[i] = -2*ifac[i]%p;
g[0]++;
for(int i=1;i<=n;++i) g[i] = (g[i]-g[i-1])%p;
int ans = 0;
for(int i=0;i<=n;++i) ans = (ans + (ll)f[i]*g[n-i])%p;
ans = (ll)(ans+p)*fac[n]%p;
if(!(n&1)) ans = p-ans;
printf("%d",ans);
return 0;
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 26412kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 0ms
memory: 26400kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 26292kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 26328kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 2ms
memory: 26404kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 0ms
memory: 26336kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 2ms
memory: 26328kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 2ms
memory: 26332kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 0ms
memory: 26288kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 2ms
memory: 26328kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 2ms
memory: 26404kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 4ms
memory: 26328kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 84ms
memory: 27652kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 184ms
memory: 30720kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: 0
Accepted
time: 397ms
memory: 34404kb
input:
131072
output:
842796122
result:
ok 1 number(s): "842796122"
Test #16:
score: 0
Accepted
time: 183ms
memory: 30860kb
input:
131071
output:
981021531
result:
ok 1 number(s): "981021531"
Test #17:
score: 0
Accepted
time: 190ms
memory: 31008kb
input:
131070
output:
480197639
result:
ok 1 number(s): "480197639"
Test #18:
score: 0
Accepted
time: 386ms
memory: 34276kb
input:
131074
output:
383000585
result:
ok 1 number(s): "383000585"
Test #19:
score: 0
Accepted
time: 389ms
memory: 34508kb
input:
131073
output:
316664839
result:
ok 1 number(s): "316664839"
Test #20:
score: 0
Accepted
time: 404ms
memory: 34312kb
input:
250000
output:
119658643
result:
ok 1 number(s): "119658643"
Test #21:
score: 0
Accepted
time: 398ms
memory: 34516kb
input:
249999
output:
78110138
result:
ok 1 number(s): "78110138"
Test #22:
score: 0
Accepted
time: 415ms
memory: 33916kb
input:
249998
output:
297253469
result:
ok 1 number(s): "297253469"
Extra Test:
score: 0
Extra Test Passed