結果
問題 | No.1239 Multiplication -2 |
ユーザー | LayCurse |
提出日時 | 2020-09-25 22:21:24 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 17,668 bytes |
コンパイル時間 | 3,122 ms |
コンパイル使用メモリ | 226,724 KB |
実行使用メモリ | 16,428 KB |
最終ジャッジ日時 | 2024-06-28 06:44:19 |
合計ジャッジ時間 | 6,405 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
12,600 KB |
testcase_01 | AC | 6 ms
10,820 KB |
testcase_02 | AC | 6 ms
12,492 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 7 ms
10,688 KB |
testcase_10 | AC | 6 ms
12,112 KB |
testcase_11 | AC | 6 ms
11,076 KB |
testcase_12 | AC | 6 ms
10,688 KB |
testcase_13 | AC | 5 ms
10,436 KB |
testcase_14 | AC | 6 ms
11,200 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | AC | 72 ms
14,048 KB |
testcase_18 | AC | 98 ms
14,460 KB |
testcase_19 | AC | 65 ms
15,560 KB |
testcase_20 | WA | - |
testcase_21 | AC | 157 ms
11,968 KB |
testcase_22 | AC | 121 ms
11,464 KB |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | AC | 8 ms
11,460 KB |
testcase_26 | AC | 214 ms
12,452 KB |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
ソースコード
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; #define MD (998244353U) #define MD_PRIMITIVE_ROOT (3U) #define PI 3.14159265358979323846 void*wmem; char memarr[96000000]; template<class S, class T> inline S max_L(S a,T b){ return a>=b?a:b; } template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } struct Modint{ unsigned val; Modint(){ val=0; } Modint(int a){ val = ord(a); } Modint(unsigned a){ val = ord(a); } Modint(long long a){ val = ord(a); } Modint(unsigned long long a){ val = ord(a); } inline unsigned ord(unsigned a){ return a%MD; } inline unsigned ord(int a){ a %= (int)MD; if(a < 0){ a += MD; } return a; } inline unsigned ord(unsigned long long a){ return a%MD; } inline unsigned ord(long long a){ a %= (int)MD; if(a < 0){ a += MD; } return a; } inline unsigned get(){ return val; } inline Modint &operator+=(Modint a){ val += a.val; if(val >= MD){ val -= MD; } return *this; } inline Modint &operator-=(Modint a){ if(val < a.val){ val = val + MD - a.val; } else{ val -= a.val; } return *this; } inline Modint &operator*=(Modint a){ val = ((unsigned long long)val*a.val)%MD; return *this; } inline Modint &operator/=(Modint a){ return *this *= a.inverse(); } inline Modint operator+(Modint a){ return Modint(*this)+=a; } inline Modint operator-(Modint a){ return Modint(*this)-=a; } inline Modint operator*(Modint a){ return Modint(*this)*=a; } inline Modint operator/(Modint a){ return Modint(*this)/=a; } inline Modint operator+(int a){ return Modint(*this)+=Modint(a); } inline Modint operator-(int a){ return Modint(*this)-=Modint(a); } inline Modint operator*(int a){ return Modint(*this)*=Modint(a); } inline Modint operator/(int a){ return Modint(*this)/=Modint(a); } inline Modint operator+(long long a){ return Modint(*this)+=Modint(a); } inline Modint operator-(long long a){ return Modint(*this)-=Modint(a); } inline Modint operator*(long long a){ return Modint(*this)*=Modint(a); } inline Modint operator/(long long a){ return Modint(*this)/=Modint(a); } inline Modint operator-(void){ Modint res; if(val){ res.val=MD-val; } else{ res.val=0; } return res; } inline operator bool(void){ return val!=0; } inline operator int(void){ return get(); } inline operator long long(void){ return get(); } inline Modint inverse(){ int a = val; int b = MD; int u = 1; int v = 0; int t; Modint res; while(b){ t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } if(u < 0){ u += MD; } res.val = u; return res; } inline Modint pw(unsigned long long b){ Modint a(*this); Modint res; res.val = 1; while(b){ if(b&1){ res *= a; } b >>= 1; a *= a; } return res; } inline bool operator==(int a){ return ord(a)==val; } inline bool operator!=(int a){ return ord(a)!=val; } } ; inline Modint operator+(int a, Modint b){ return Modint(a)+=b; } inline Modint operator-(int a, Modint b){ return Modint(a)-=b; } inline Modint operator*(int a, Modint b){ return Modint(a)*=b; } inline Modint operator/(int a, Modint b){ return Modint(a)/=b; } inline Modint operator+(long long a, Modint b){ return Modint(a)+=b; } inline Modint operator-(long long a, Modint b){ return Modint(a)-=b; } inline Modint operator*(long long a, Modint b){ return Modint(a)*=b; } inline Modint operator/(long long a, Modint b){ return Modint(a)/=b; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(Modint x){ int i; i = (int)x; wt_L(i); } template<class T, class S> inline T pow_L(T a, S b){ T res = 1; res = 1; for(;;){ if(b&1){ res *= a; } b >>= 1; if(b==0){ break; } a *= a; } return res; } inline double pow_L(double a, double b){ return pow(a,b); } struct fft_pnt{ double x; double y; fft_pnt(void){ } fft_pnt(double a, double b){ x = a; y = b; } void set(double a, double b){ x = a; y = b; } fft_pnt& operator+=(fft_pnt a){ x+=a.x; y+=a.y; return *this; } fft_pnt& operator-=(fft_pnt a){ x-=a.x; y-=a.y; return *this; } fft_pnt& operator*=(fft_pnt a){ fft_pnt p = *this; x = p.x*a.x-p.y*a.y; y = p.x*a.y+p.y*a.x; return *this; } fft_pnt operator+(fft_pnt a){ return fft_pnt(*this) += a; } fft_pnt operator-(fft_pnt a){ return fft_pnt(*this) -= a; } fft_pnt operator*(fft_pnt a){ return fft_pnt(*this) *= a; } } ; void fft_L(int n, fft_pnt x[], void *mem){ int i; int j; int n1; int n2; int n3; int step = 1; double theta = 2*PI / n; double tmp; fft_pnt w1; fft_pnt w2; fft_pnt w3; fft_pnt a; fft_pnt b; fft_pnt c; fft_pnt d; fft_pnt aa; fft_pnt bb; fft_pnt cc; fft_pnt dd; fft_pnt*y = (fft_pnt*)mem; while(n > 2){ n1 = n / 4; n2 = n1 + n1; n3 = n1 + n2; for(i=(0);i<(n1);i++){ w1 = fft_pnt(cos(i*theta),-sin(i*theta)); w2 = w1*w1; w3 = w1*w2; for(j=(0);j<(step);j++){ a = x[j+step*i]; b = x[j+step*(i+n1)]; c = x[j+step*(i+n2)]; d = x[j+step*(i+n3)]; aa = a + c; bb = a - c; cc = b + d; dd = b - d; tmp = dd.y; dd.y = dd.x; dd.x = -tmp; y[j+step*(4*i )] = aa + cc; y[j+step*(4*i+1)] = w1*(bb - dd); y[j+step*(4*i+2)] = w2*(aa - cc); y[j+step*(4*i+3)] = w3*(bb + dd); } } n /= 4; step *= 4; theta *= 4; swap(x,y); } if(n==2){ for(i=(0);i<(step);i++){ y[i] = x[i] + x[i+step]; y[i+step] = x[i] - x[i+step]; } n /= 2; step *= 2; theta *= 2; swap(x,y); } for(i=(0);i<(step);i++){ y[i] = x[i]; } } void fftinv_L(int n, fft_pnt x[], void *mem){ int i; int j; int n1; int n2; int n3; int step = 1; double theta = 2*PI / n; double tmp; fft_pnt w1; fft_pnt w2; fft_pnt w3; fft_pnt a; fft_pnt b; fft_pnt c; fft_pnt d; fft_pnt aa; fft_pnt bb; fft_pnt cc; fft_pnt dd; fft_pnt*y = (fft_pnt*)mem; while(n > 2){ n1 = n / 4; n2 = n1 + n1; n3 = n1 + n2; for(i=(0);i<(n1);i++){ w1 = fft_pnt(cos(i*theta),sin(i*theta)); w2 = w1*w1; w3 = w1*w2; for(j=(0);j<(step);j++){ a = x[j+step*i]; b = x[j+step*(i+n1)]; c = x[j+step*(i+n2)]; d = x[j+step*(i+n3)]; aa = a + c; bb = a - c; cc = b + d; dd = b - d; tmp = dd.y; dd.y = dd.x; dd.x = -tmp; y[j+step*(4*i )] = aa + cc; y[j+step*(4*i+1)] = w1*(bb + dd); y[j+step*(4*i+2)] = w2*(aa - cc); y[j+step*(4*i+3)] = w3*(bb - dd); } } n /= 4; step *= 4; theta *= 4; swap(x,y); } if(n==2){ for(i=(0);i<(step);i++){ y[i] = x[i] + x[i+step]; y[i+step] = x[i] - x[i+step]; } n /= 2; step *= 2; theta *= 2; swap(x,y); } for(i=(0);i<(step);i++){ y[i] = x[i]; } } void convolution_L(double A[], int As, double B[], int Bs, double res[], int Rs, void *mem = wmem){ int i; int n; int n2; double mul; fft_pnt*a; fft_pnt*b; n =max_L(As+Bs, Rs); for(n2=1;n2<n;n2*=2){ ; } walloc1d(&a, n2, &mem); walloc1d(&b, n2, &mem); for(i=(0);i<(As);i++){ a[i].set(A[i], 0); } int KaFyNJB9 = n2; for(i=(As);i<(KaFyNJB9);i++){ a[i].set(0,0); } for(i=(0);i<(Bs);i++){ b[i].set(B[i], 0); } int jO2HaRTX = n2; for(i=(Bs);i<(jO2HaRTX);i++){ b[i].set(0,0); } fft_L(n2, a, mem); fft_L(n2, b, mem); for(i=(0);i<(n2);i++){ a[i] *= b[i]; } fftinv_L(n2, a, mem); mul = 1.0 / n2; for(i=(0);i<(Rs);i++){ res[i] = a[i].x * mul; } } void convolution_L(double A[], int As, double res[], int Rs, void *mem = wmem){ int i; int n; int n2; double mul; fft_pnt*a; n =max_L(As+As, Rs); for(n2=1;n2<n;n2*=2){ ; } walloc1d(&a, n2, &mem); for(i=(0);i<(As);i++){ a[i].set(A[i], 0); } int eNrGll8F = n2; for(i=(As);i<(eNrGll8F);i++){ a[i].set(0,0); } fft_L(n2, a, mem); for(i=(0);i<(n2);i++){ a[i] *= a[i]; } fftinv_L(n2, a, mem); mul = 1.0 / n2; for(i=(0);i<(Rs);i++){ res[i] = a[i].x * mul; } } void fft_L(int n, Modint x[], Modint root, void *mem){ int i; int j; int n1; int n2; int n3; int step = 1; Modint w1; Modint w2; Modint w3; Modint a; Modint b; Modint c; Modint d; Modint aa; Modint bb; Modint cc; Modint dd; Modint tmp; Modint*y; walloc1d(&y, n, &mem); tmp = root.pw((MD-1)/4*3); root = root.pw((MD-1)/n); while(n > 2){ n1 = n / 4; n2 = n1 + n1; n3 = n1 + n2; w1.val = 1; for(i=(0);i<(n1);i++){ w2 = w1*w1; w3 = w1*w2; for(j=(0);j<(step);j++){ a = x[j+step*i]; b = x[j+step*(i+n1)]; c = x[j+step*(i+n2)]; d = x[j+step*(i+n3)]; aa = a + c; bb = a - c; cc = b + d; dd = (b - d) * tmp; y[j+step*(4*i )] = aa + cc; y[j+step*(4*i+1)] = w1*(bb - dd); y[j+step*(4*i+2)] = w2*(aa - cc); y[j+step*(4*i+3)] = w3*(bb + dd); } w1 *= root; } n /= 4; step *= 4; root *= root; root *= root; swap(x,y); } if(n==2){ for(i=(0);i<(step);i++){ y[i] = x[i] + x[i+step]; y[i+step] = x[i] - x[i+step]; } n /= 2; step *= 2; root *= root; swap(x,y); } for(i=(0);i<(step);i++){ y[i] = x[i]; } } void fftinv_L(int n, Modint x[], Modint root, void *mem){ int i; int j; int n1; int n2; int n3; int step = 1; Modint w1; Modint w2; Modint w3; Modint a; Modint b; Modint c; Modint d; Modint aa; Modint bb; Modint cc; Modint dd; Modint tmp; Modint*y; walloc1d(&y, n, &mem); root = root.inverse(); tmp = root.pw((MD-1)/4); root = root.pw((MD-1)/n); while(n > 2){ n1 = n / 4; n2 = n1 + n1; n3 = n1 + n2; w1.val = 1; for(i=(0);i<(n1);i++){ w2 = w1*w1; w3 = w1*w2; for(j=(0);j<(step);j++){ a = x[j+step*i]; b = x[j+step*(i+n1)]; c = x[j+step*(i+n2)]; d = x[j+step*(i+n3)]; aa = a + c; bb = a - c; cc = b + d; dd = (b - d) * tmp; y[j+step*(4*i )] = aa + cc; y[j+step*(4*i+1)] = w1*(bb + dd); y[j+step*(4*i+2)] = w2*(aa - cc); y[j+step*(4*i+3)] = w3*(bb - dd); } w1 *= root; } n /= 4; step *= 4; root *= root; root *= root; swap(x,y); } if(n==2){ for(i=(0);i<(step);i++){ y[i] = x[i] + x[i+step]; y[i+step] = x[i] - x[i+step]; } n /= 2; step *= 2; root *= root; swap(x,y); } for(i=(0);i<(step);i++){ y[i] = x[i]; } } void convolution_L(Modint A[], int As, Modint B[], int Bs, Modint res[], int Rs, Modint root = MD_PRIMITIVE_ROOT, void *mem = wmem){ int i; int n; int n2; Modint*a; Modint*b; Modint r; n =max_L(As+Bs, Rs); for(n2=1;n2<n;n2*=2){ ; } walloc1d(&a, n2, &mem); walloc1d(&b, n2, &mem); for(i=(0);i<(As);i++){ a[i] = A[i]; } int CnS5KYSU = n2; for(i=(As);i<(CnS5KYSU);i++){ a[i].val = 0; } for(i=(0);i<(Bs);i++){ b[i] = B[i]; } int YtJecZqT = n2; for(i=(Bs);i<(YtJecZqT);i++){ b[i].val = 0; } fft_L(n2, a, root, mem); fft_L(n2, b, root, mem); for(i=(0);i<(n2);i++){ a[i] *= b[i]; } fftinv_L(n2, a, root, mem); r = Modint(n2).inverse(); for(i=(0);i<(Rs);i++){ res[i] = a[i] * r; } } void convolution_L(Modint A[], int As, Modint res[], int Rs, Modint root = MD_PRIMITIVE_ROOT, void *mem = wmem){ int i; int n; int n2; Modint*a; Modint r; n =max_L(2*As, Rs); for(n2=1;n2<n;n2*=2){ ; } walloc1d(&a, n2, &mem); for(i=(0);i<(As);i++){ a[i] = A[i]; } int jIDgiLP1 = n2; for(i=(As);i<(jIDgiLP1);i++){ a[i].val = 0; } fft_L(n2, a, root, mem); for(i=(0);i<(n2);i++){ a[i] *= a[i]; } fftinv_L(n2, a, root, mem); r = Modint(n2).inverse(); for(i=(0);i<(Rs);i++){ res[i] = a[i]*r; } } int N; int A[200000]; int ls; Modint lp[200000+1]; Modint lm[200000+1]; int rs; Modint rp[200000+1]; Modint rm[200000+1]; Modint c1[200000+1]; Modint c2[200000+1]; int main(){ wmem = memarr; int i; int j; int k; int ad; Modint res = 0; rd(N); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){ rd(A[Lj4PdHRW]); } } for(i=(0);i<(N);i++){ if(A[i]==2 || A[i]==-2){ ls = 1; lp[0] = 1; lm[0] = 0; j = i; for(;;){ j--; if(j < 0 || A[j] == 0 || A[j] == 2 || A[j] == -2){ break; } lp[ls] = lp[ls-1]; lm[ls] = lm[ls-1]; if(A[j]==-1){ swap(lp[ls], lm[ls]); } ls++; } if(ls > 1 && j==-1){ lp[ls-2] += lp[ls-1]; lm[ls-2] += lm[ls-1]; ls--; } rs = 1; rp[0] = 1; rm[0] = 0; j = i; for(;;){ j++; if(j >= N || A[j] == 0 || A[j] == 2 || A[j] == -2){ break; } rp[rs] = rp[rs-1]; rm[rs] = rm[rs-1]; if(A[j]==-1){ swap(rp[ls], rm[ls]); } rs++; } if(rs > 1 && j==N){ rp[rs-2] += rp[rs-1]; rm[rs-2] += rm[rs-1]; rs--; } if(A[i]==2){ convolution_L(lp, ls, rm, rs, c1, ls+rs); convolution_L(lm, ls, rp, rs, c2, ls+rs); } else{ convolution_L(lp, ls, rp, rs, c1, ls+rs); convolution_L(lm, ls, rm, rs, c2, ls+rs); } ad = 0; if(i==0){ ad++; } if(i==N-1){ ad++; } for(k=(0);k<(ls+rs);k++){ c1[k] += c2[k]; } for(k=(0);k<(ls+rs);k++){ if(c1[k] != 0){ res += c1[k] * ((pow_L(Modint(2),(N-3-k+ad)))); } } } } res /=(pow_L(Modint(2),(N - 1))); wt_L(res); wt_L('\n'); return 0; } // cLay varsion 20200920-1 // --- original code --- // #define MD 998244353 // int N, A[2d5]; // // int ls; Modint lp[2d5+1], lm[2d5+1]; // int rs; Modint rp[2d5+1], rm[2d5+1]; // Modint c1[2d5+1], c2[2d5+1]; // // { // int i, j, k, ad; // Modint res = 0; // rd(N,A(N)); // // rep(i,N) if(A[i]==2 || A[i]==-2){ // ls = 1; // lp[0] = 1; lm[0] = 0; // j = i; // for(;;){ // j--; // if(j < 0 || A[j] == 0 || A[j] == 2 || A[j] == -2) break; // lp[ls] = lp[ls-1]; // lm[ls] = lm[ls-1]; // if(A[j]==-1) swap(lp[ls], lm[ls]); // ls++; // } // if(ls > 1 && j==-1){ // lp[ls-2] += lp[ls-1]; // lm[ls-2] += lm[ls-1]; // ls--; // } // // rs = 1; // rp[0] = 1; rm[0] = 0; // j = i; // for(;;){ // j++; // if(j >= N || A[j] == 0 || A[j] == 2 || A[j] == -2) break; // rp[rs] = rp[rs-1]; // rm[rs] = rm[rs-1]; // if(A[j]==-1) swap(rp[ls], rm[ls]); // rs++; // } // if(rs > 1 && j==N){ // rp[rs-2] += rp[rs-1]; // rm[rs-2] += rm[rs-1]; // rs--; // } // // //wt("i = ", i, A[i]); // //wt("lp", lp(ls)); // //wt("lm", lm(ls)); // //wt("rp", rp(rs)); // //wt("rm", rm(rs)); // // if(A[i]==2){ // convolution(lp, ls, rm, rs, c1, ls+rs); // convolution(lm, ls, rp, rs, c2, ls+rs); // } else { // convolution(lp, ls, rp, rs, c1, ls+rs); // convolution(lm, ls, rm, rs, c2, ls+rs); // } // // ad = 0; // if(i==0) ad++; // if(i==N-1) ad++; // // rep(k,ls+rs) c1[k] += c2[k]; // //wt("c", c1(ls+rs)); // rep(k,ls+rs) if(c1[k] != 0){ // res += c1[k] * (Modint(2) ** (N-3-k+ad)); // } // //wt("res", res); // } // // res /= Modint(2) ** (N - 1); // wt(res); // }