結果

問題 No.1239 Multiplication -2
ユーザー pockynypockyny
提出日時 2020-09-25 22:13:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,673 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 71,020 KB
実行使用メモリ 5,812 KB
最終ジャッジ日時 2023-09-10 15:27:41
合計ジャッジ時間 3,509 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 109 ms
5,704 KB
testcase_18 AC 106 ms
5,688 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 103 ms
5,756 KB
testcase_22 AC 88 ms
5,632 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 23 ms
4,808 KB
testcase_26 AC 69 ms
4,824 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 -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

using namespace std;
typedef long long ll;
ll mod = 998244353,inv[200010],b[4] = {},ans1 = 0,ans2 = 0;
int 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]); 
    for(i=1;i<n - 1;i++){
        c *= a[i];
        if(c>2 || c<-2) 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) 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]++;
        }
        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;
    }
    if(n>=3) (ans2 *= inv[2]) %= mod;
    cout << (ans1 + ans2)%mod << endl;
}
0