結果
問題 | No.1239 Multiplication -2 |
ユーザー |
![]() |
提出日時 | 2020-09-25 22:29:02 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 25 ms / 2,000 ms |
コード長 | 1,983 bytes |
コンパイル時間 | 1,324 ms |
コンパイル使用メモリ | 98,756 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-28 06:54:42 |
合計ジャッジ時間 | 2,678 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=998244353; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} ll mod_pow(ll x,ll n){ x%=MOD; ll res=1; while(n>0){ if(n&1) res=res*x%MOD; x=x*x%MOD; n>>=1; } return res; } ll mod_inverse(ll x){ return mod_pow(x,MOD-2); } void add(ll &a,ll b){ a=(a+b)%MOD; } void mul(ll &a,ll b){ a%=MOD;b%=MOD; a=a*b%MOD; } ll dp[200020][3][2]; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; vector<int> A(N); rep(i,N) cin>>A[i]; vector<ll> two(N+1,0); two[0]=1; rep(i,N) two[i+1]=two[i]*2%MOD; ll ans=0; for(int i=0;i<N;i++){ if(abs(A[i])!=2) continue; vector<ll> lef(2,0); if(i==0) lef[0]=1; else lef[0]=two[i-1]; { int pt=0; for(int j=i-1;j>=0;j--){ if(abs(A[j])!=1) break; if(A[j]==-1) pt=1-pt; ll res=1; if(j>0) mul(res,two[j-1]); add(lef[pt],res); } } vector<ll> rig(2,0); if(i==N-1) rig[0]=1; else rig[0]=two[N-i-2]; { int pt=0; for(int j=i+1;j<N;j++){ if(abs(A[j])!=1) break; if(A[j]==-1) pt=1-pt; ll res=1; if(j<N-1) mul(res,two[N-j-2]); add(rig[pt],res); } } ll res=0; if(A[i]>0) res=lef[0]*rig[1]+lef[1]*rig[0]; else res=lef[0]*rig[0]+lef[1]*rig[1]; res%=MOD; add(ans,res); } ll coef=mod_pow(2,N-1); coef=mod_inverse(coef); mul(ans,coef); cout<<ans<<endl; return 0; }