QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#421627 | #8721. 括号序列 | warzone | AC ✓ | 1311ms | 21916kb | C++14 | 3.8kb | 2024-05-25 22:46:05 | 2024-05-25 22:46:06 |
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 19
#define mx_ 18
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;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11588kb
input:
3
output:
28
result:
ok 1 number(s): "28"
Test #2:
score: 0
Accepted
time: 5ms
memory: 10668kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 5ms
memory: 9736kb
input:
2
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 5ms
memory: 9728kb
input:
4
output:
282
result:
ok 1 number(s): "282"
Test #5:
score: 0
Accepted
time: 0ms
memory: 9732kb
input:
5
output:
3718
result:
ok 1 number(s): "3718"
Test #6:
score: 0
Accepted
time: 2ms
memory: 9728kb
input:
6
output:
60694
result:
ok 1 number(s): "60694"
Test #7:
score: 0
Accepted
time: 0ms
memory: 9744kb
input:
7
output:
1182522
result:
ok 1 number(s): "1182522"
Test #8:
score: 0
Accepted
time: 2ms
memory: 9640kb
input:
8
output:
26791738
result:
ok 1 number(s): "26791738"
Test #9:
score: 0
Accepted
time: 5ms
memory: 9704kb
input:
9
output:
692310518
result:
ok 1 number(s): "692310518"
Test #10:
score: 0
Accepted
time: 2ms
memory: 9704kb
input:
10
output:
135061370
result:
ok 1 number(s): "135061370"
Test #11:
score: 0
Accepted
time: 2ms
memory: 9736kb
input:
100
output:
423669705
result:
ok 1 number(s): "423669705"
Test #12:
score: 0
Accepted
time: 11ms
memory: 9900kb
input:
1234
output:
878522960
result:
ok 1 number(s): "878522960"
Test #13:
score: 0
Accepted
time: 282ms
memory: 12704kb
input:
54321
output:
827950477
result:
ok 1 number(s): "827950477"
Test #14:
score: 0
Accepted
time: 598ms
memory: 15360kb
input:
65536
output:
380835743
result:
ok 1 number(s): "380835743"
Test #15:
score: 0
Accepted
time: 1293ms
memory: 20944kb
input:
131072
output:
842796122
result:
ok 1 number(s): "842796122"
Test #16:
score: 0
Accepted
time: 634ms
memory: 15892kb
input:
131071
output:
981021531
result:
ok 1 number(s): "981021531"
Test #17:
score: 0
Accepted
time: 599ms
memory: 15876kb
input:
131070
output:
480197639
result:
ok 1 number(s): "480197639"
Test #18:
score: 0
Accepted
time: 1311ms
memory: 20988kb
input:
131074
output:
383000585
result:
ok 1 number(s): "383000585"
Test #19:
score: 0
Accepted
time: 1300ms
memory: 20960kb
input:
131073
output:
316664839
result:
ok 1 number(s): "316664839"
Test #20:
score: 0
Accepted
time: 1307ms
memory: 21904kb
input:
250000
output:
119658643
result:
ok 1 number(s): "119658643"
Test #21:
score: 0
Accepted
time: 1305ms
memory: 21916kb
input:
249999
output:
78110138
result:
ok 1 number(s): "78110138"
Test #22:
score: 0
Accepted
time: 1291ms
memory: 21888kb
input:
249998
output:
297253469
result:
ok 1 number(s): "297253469"
Extra Test:
score: 0
Extra Test Passed