結果

問題 No.1239 Multiplication -2
ユーザー msm1993msm1993
提出日時 2020-10-06 11:51:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 1,942 bytes
コンパイル時間 632 ms
コンパイル使用メモリ 71,192 KB
実行使用メモリ 32,000 KB
最終ジャッジ日時 2023-09-27 06:31:55
合計ジャッジ時間 4,260 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 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,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 14 ms
12,256 KB
testcase_16 AC 21 ms
18,236 KB
testcase_17 AC 33 ms
31,996 KB
testcase_18 AC 33 ms
31,916 KB
testcase_19 AC 33 ms
31,996 KB
testcase_20 AC 36 ms
32,000 KB
testcase_21 AC 32 ms
31,272 KB
testcase_22 AC 32 ms
30,220 KB
testcase_23 AC 24 ms
23,260 KB
testcase_24 AC 25 ms
22,240 KB
testcase_25 AC 20 ms
19,612 KB
testcase_26 AC 23 ms
21,044 KB
testcase_27 AC 13 ms
10,884 KB
testcase_28 AC 33 ms
27,656 KB
testcase_29 AC 34 ms
29,664 KB
testcase_30 AC 17 ms
14,380 KB
testcase_31 AC 23 ms
19,996 KB
testcase_32 AC 37 ms
31,736 KB
testcase_33 AC 26 ms
23,048 KB
testcase_34 AC 24 ms
21,500 KB
testcase_35 AC 13 ms
12,304 KB
testcase_36 AC 13 ms
11,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
const int MOD = 998244353;
long long extgcd(long long a, long long b, long long &x, long long &y){
    int d = a;
    if(b != 0){
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }
    else{
        x = 1;
        y = 0;
    }
    return d;
}
long long repow(long long x, long long y){
    if(y == 0) return 1;
    long long res = 1;
    while(y > 0){
        if(y & 1) res = res * x % MOD;
        x = x * x % MOD;
        y >>= 1;
    }
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<int> a(N);
    for(int i = 0; i < N; i++) cin >> a[i];
    long long Q = repow(2, N - 1);
    long long dp[N + 1][2][3][3];
    for(int i = 0; i < N + 1; i++){
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 3; k++){
                for(int l = 0; l < 3; l++) dp[i][j][k][l] = 0;
            }
        }
    }
    dp[0][0][0][0] = 1;
    for(int i = 0; i < N; i++){
        int neg = (a[i] < 0) ? 1 : 0;
        int two = (a[i] == 0) ? 2 : (abs(a[i]) % 2 == 0) ? 1 : 0;
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 3; k++){
                if(i == 0) dp[i + 1][j][k][0] = dp[i][j][k][0];
                else dp[i + 1][j][k][0] = 2 * dp[i][j][k][0] % MOD;

                dp[i + 1][(j + neg) % 2][min(2, k + two)][1] += dp[i][j][k][0] + dp[i][j][k][1];
                dp[i + 1][(j + neg) % 2][min(2, k + two)][2] += dp[i][j][k][0] + dp[i][j][k][1];
                dp[i + 1][(j + neg) % 2][min(2, k + two)][1] %= MOD;
                dp[i + 1][(j + neg) % 2][min(2, k + two)][2] %= MOD;

                if(i == N - 1) dp[i + 1][j][k][2] += dp[i][j][k][2];
                else dp[i + 1][j][k][2] += dp[i][j][k][2] * 2 % MOD;
                dp[i + 1][j][k][2] %= MOD;
            }
        }
    }
    cout << dp[N][1][1][2] * repow(Q, MOD - 2) % MOD << endl;
}
0