QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#421626 | #8721. 括号序列 | warzone | RE | 590ms | 11668kb | C++14 | 3.8kb | 2024-05-25 22:45:24 | 2024-05-25 22:45:25 |
Judging History
answer
#include<stdio.h>
#include<string.h>
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int word;
typedef unsigned short hword;
typedef unsigned char byte;
#define mx 18
#define mx_ 17
word realid[1<<(mx+1)];
#define id(size,i) realid[(i)|(size)]
#define FOR(size) for(word i=0;i<(size);++i)
#define fftfor(size) \
for(word floor=1;floor<(size);floor<<=1) \
for(word head=0;head<(size);head+=floor<<1) \
for(word i=0;i<floor;++i)
const word mod=998244353,nimod=29;
inline word qpow(word a,word b){
word ans=1;
for(;b;b>>=1){
if(b&1) ans=1ull*ans*a%mod;
a=1ull*a*a%mod;
}
return ans;
}
word root[1<<mx],inv[1<<mx],ni[nimod];
struct READ{
char c;
inline READ(){
c=getchar();
ni[1]=1,ni[2]=qpow(2,mod-2);
for(word floor=1,i=2;floor<=mx;++floor){
ni[(1u<<floor)%nimod]=1ull*ni[(1u<<(floor-1))%nimod]*ni[2]%mod;
for(;i<(1u<<(floor+1));++i)
realid[i]=(i&1)<<(floor-1)|realid[i>>1];
}
const word num1=qpow(3,(mod-1)>>mx);
const word num2=qpow(num1,mod-2);
root[1<<mx_]=inv[1<<mx_]=1;
for(word i=(1<<mx_)+1;i<(1<<mx);++i){
root[i]=1ull*num1*root[i-1]%mod;
inv[i]=1ull*num2*inv[i-1]%mod;
}
for(word i=(1u<<mx_)-1;i;--i)
root[i]=root[i<<1],inv[i]=inv[i<<1];
}
template<typename type>
inline READ& operator >>(type& num){
while('0'>c||c>'9') c=getchar();
for(num=0;'0'<=c&&c<='9';c=getchar())
num=num*10+(c-'0');
return *this;
}
}cin;
#define ntt(num,root)( \
num1=num[head|i],num2=num[head|i|floor], \
num1+=(num2=1ull*num2*root[i|floor]%mod), \
num[head|i]=num1>=mod? num1-mod:num1, \
num1-=num2,num1+=mod-num2, \
num[head|i|floor]=num1>=mod? num1-mod:num1)
namespace FFT{
struct fftreg{
word ecx[1<<mx];
template<typename type>
inline void operator()(const word size,const type& f){
FOR(size>>1){
ecx[id(size,i)]=f[i];
ecx[id(size,size>>1|i)]=0;
}
word num1,num2;
fftfor(size) ntt(ecx,root);
}
};
inline void conv(const word size,const fftreg &f,
const fftreg &g,word *const f_times_g){
FOR(size) f_times_g[id(size,i)]=1ull*f.ecx[i]*g.ecx[i]%mod;
word num1,num2;
fftfor(size) ntt(f_times_g,inv);
num1=ni[size%nimod];
FOR(size) f_times_g[i]=1ull*num1*f_times_g[i]%mod;
}
}
using FFT::fftreg;
using FFT::conv;
word in[1<<mx],eax[1<<mx],ebx[1<<mx];
fftreg ecx,edx;
#define newton(size_) \
word size=2; \
while(size<<=1,(size>>2)<(size_))
inline void _1(const word size_){
ebx[0]=qpow(eax[0],mod-2);
newton(size_){
ecx(size,eax),edx(size,ebx);
conv(size,ecx,edx,ebx);
FOR(size) ebx[i]=(mod-ebx[i])%mod;
ebx[0]=(2+ebx[0])%mod;
ecx(size,ebx);
conv(size,ecx,edx,ebx);
}
}
word fact[1u<<mx_],rev[1u<<mx_];
inline void ln(const word size){
_1(size),ecx(size<<1,ebx);
FOR(size) ebx[i]=(1ull+i)*eax[i+1]%mod;
edx(size<<1,ebx),conv(size<<1,ecx,edx,ebx);
for(word i=size-1;i;--i)
ebx[i]=1ull*rev[i]*fact[i-1]%mod*ebx[i-1]%mod;
ebx[0]=0;
}
inline void exp_(const word size_){
eax[0]=1;
newton(size_){
ln(size>>1);
FOR(size>>1) ebx[i]=(in[i]+mod-ebx[i])%mod;
ebx[0]=(ebx[0]+1)%mod;
ecx(size,eax),edx(size,ebx);
conv(size,ecx,edx,eax);
}
}
inline void solve(const word size_){
in[0]=0;
newton(size_){
exp_(size>>1);
(in[0]+=mod-1)%=mod;
ecx(size,in),edx(size,eax);
(eax[0]+=mod-ni[2])%=mod;
conv(size,ecx,edx,in),_1(size>>1);
(++in[0])%=mod,(in[1]+=mod-ni[2])%=mod;
ecx(size,in),edx(size,ebx);
conv(size,ecx,edx,in);
}
}
int main(){
word n,size=1;
cin>>n,fact[0]=inv[0]=1;
for(word i=1;i<=n;++i)
fact[i]=1ull*i*fact[i-1]%mod;
rev[n]=qpow(fact[n],mod-2);
for(word i=n;i;--i)
rev[i-1]=1ull*i*rev[i]%mod;
for(;size<=n;size<<=1);
solve(size);
FOR(size) eax[i]=in[i];
(++eax[0])%=mod,_1(size);
printf("%u",1ull*ebx[n]*fact[n]%mod);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 10124kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 0ms
memory: 11164kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 2ms
memory: 10036kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 10200kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 3ms
memory: 11088kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 2ms
memory: 9980kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 2ms
memory: 9868kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 0ms
memory: 9956kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 0ms
memory: 11348kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 2ms
memory: 9680kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 3ms
memory: 9820kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 6ms
memory: 10996kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 277ms
memory: 11668kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 590ms
memory: 11212kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: -100
Runtime Error
input:
131072