結果

問題 No.1239 Multiplication -2
ユーザー nawawannawawan
提出日時 2020-09-26 14:31:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,942 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 70,992 KB
実行使用メモリ 32,072 KB
最終ジャッジ日時 2023-09-11 11:18:19
合計ジャッジ時間 3,134 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 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 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 13 ms
12,200 KB
testcase_16 AC 21 ms
18,264 KB
testcase_17 AC 32 ms
32,060 KB
testcase_18 AC 33 ms
31,916 KB
testcase_19 AC 34 ms
32,072 KB
testcase_20 AC 35 ms
31,984 KB
testcase_21 AC 32 ms
31,268 KB
testcase_22 AC 32 ms
30,288 KB
testcase_23 AC 25 ms
23,360 KB
testcase_24 AC 24 ms
22,320 KB
testcase_25 AC 20 ms
19,640 KB
testcase_26 AC 21 ms
20,936 KB
testcase_27 AC 12 ms
10,976 KB
testcase_28 AC 31 ms
27,684 KB
testcase_29 AC 34 ms
29,960 KB
testcase_30 AC 16 ms
14,408 KB
testcase_31 AC 22 ms
19,952 KB
testcase_32 AC 36 ms
31,800 KB
testcase_33 AC 26 ms
23,012 KB
testcase_34 AC 24 ms
21,420 KB
testcase_35 AC 13 ms
12,404 KB
testcase_36 AC 12 ms
11,296 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