結果

問題 No.1239 Multiplication -2
ユーザー pockynypockyny
提出日時 2020-09-25 22:25:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 1,749 bytes
コンパイル時間 609 ms
コンパイル使用メモリ 70,544 KB
実行使用メモリ 6,580 KB
最終ジャッジ日時 2023-09-10 15:40:25
合計ジャッジ時間 3,633 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 29 ms
4,452 KB
testcase_16 AC 49 ms
5,008 KB
testcase_17 AC 110 ms
6,524 KB
testcase_18 AC 108 ms
6,488 KB
testcase_19 AC 106 ms
6,544 KB
testcase_20 AC 114 ms
6,544 KB
testcase_21 AC 104 ms
6,424 KB
testcase_22 AC 90 ms
6,400 KB
testcase_23 AC 78 ms
5,604 KB
testcase_24 AC 67 ms
5,636 KB
testcase_25 AC 23 ms
5,140 KB
testcase_26 AC 70 ms
5,492 KB
testcase_27 AC 29 ms
4,380 KB
testcase_28 AC 94 ms
6,096 KB
testcase_29 AC 103 ms
6,580 KB
testcase_30 AC 41 ms
4,780 KB
testcase_31 AC 62 ms
5,500 KB
testcase_32 AC 105 ms
6,524 KB
testcase_33 AC 73 ms
5,704 KB
testcase_34 AC 67 ms
5,432 KB
testcase_35 AC 33 ms
4,380 KB
testcase_36 AC 29 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

using namespace std;
typedef long long ll;
ll mod = 998244353,inv[200010],b[4] = {},ans1 = 0,ans2 = 0,a[200010];
ll pw(ll a,ll x){
    ll ret = 1;
    while(x){
        if(x&1) (ret *= a) %= mod;
        (a *= a) %= mod; x /= 2;
    }
    return ret;
}

int main(){
    int i,n; cin >> n;
    for(i=0;i<n;i++) cin >> a[i];
    if(n==1){
        if(a[0]==-2) cout << 1 << endl;
        else cout << 0 << endl;
        return 0;
    }
    inv[0] = 1,inv[1] = (mod + 1)/2;
    for(i=2;i<=n;i++) inv[i] = inv[i - 1]*inv[1]%mod;
    ll c = a[0];
    if(c==-2) (ans1 += inv[1]) %= mod; 
    for(i=1;i<n - 1;i++){
        c *= a[i];
        if(c>2 || c<-2 || c==0) break;
        if(c==-2) (ans1 += inv[i + 1]) %= mod;
    }
    c = a[n - 1];
    if(c==-2) (ans1 += inv[1]) %= mod;
    for(i=n - 2;i>=1;i--){
        c *= a[i];
        if(c>2 || c<-2 || c==0) break;
        if(c==-2) (ans1 += inv[n - i]) %= mod;
    }
    c = 1;
    for(i=0;i<n;i++){
        c *= a[i];
        if(c>2 || c<-2) break;
    }
    if(c==-2) (ans1 += inv[n - 1]) %= mod;
    //b[0]=-2,b[1]=-1,b[2]=1,b[3]=2
    for(i=1;i<n - 1;i++){
        if(a[i]==-2){
            b[0] = (b[2] + pw(2,i))%mod; b[3] = b[1]; b[1] = b[2] = 0;
        }
        if(a[i]==-1){
            swap(b[0],b[3]); swap(b[1],b[2]); (b[1] += pw(2,i)) %= mod;
        }
        if(a[i]==0){
            b[0] = b[1] = b[2] = b[3] = 0;
        }
        if(a[i]==1){
            (b[2] += pw(2,i)) %= mod;
        }
        if(a[i]==2){
            (b[3] = b[2] + pw(2,i)) %= mod; b[0] = b[1]; b[1] = b[2] = 0;
        }
        (ans2 += inv[i]*b[0]%mod) %= mod;
    }
    (ans2 *= inv[2]) %= mod;
    //cout << ans1 << " " << ans2*4%mod << endl;
    cout << (ans1 + ans2)%mod << endl;
}
0