結果

問題 No.1239 Multiplication -2
ユーザー MisterMister
提出日時 2020-09-26 14:05:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,392 bytes
コンパイル時間 975 ms
コンパイル使用メモリ 78,992 KB
実行使用メモリ 4,660 KB
最終ジャッジ日時 2023-09-11 10:44:33
合計ジャッジ時間 3,719 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 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 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 12 ms
4,376 KB
testcase_17 AC 15 ms
4,544 KB
testcase_18 AC 16 ms
4,624 KB
testcase_19 AC 16 ms
4,600 KB
testcase_20 AC 17 ms
4,620 KB
testcase_21 AC 19 ms
4,584 KB
testcase_22 AC 19 ms
4,632 KB
testcase_23 AC 16 ms
4,376 KB
testcase_24 AC 15 ms
4,380 KB
testcase_25 AC 9 ms
4,380 KB
testcase_26 AC 16 ms
4,380 KB
testcase_27 AC 7 ms
4,380 KB
testcase_28 AC 19 ms
4,376 KB
testcase_29 AC 21 ms
4,660 KB
testcase_30 AC 10 ms
4,380 KB
testcase_31 AC 14 ms
4,380 KB
testcase_32 AC 22 ms
4,596 KB
testcase_33 AC 15 ms
4,376 KB
testcase_34 AC 15 ms
4,376 KB
testcase_35 AC 8 ms
4,376 KB
testcase_36 AC 7 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <atcoder/modint>

namespace ac = atcoder;
using mint = ac::modint998244353;

void solve() {
    int n;
    std::cin >> n;

    std::vector<int> xs(n);
    for (auto& x : xs) std::cin >> x;

    std::vector<mint> inv2(n + 1, 0);
    inv2[n] = mint(2).pow(n).inv();
    for (int i = n - 1; i >= 0; --i) inv2[i] = inv2[i + 1] * 2;

    mint ans = 0;
    for (int s = 0; s < n; ++s) {
        if (std::abs(xs[s]) != 2) continue;

        std::vector<mint> left(2, 0);
        {
            int sign = 0, bar = (s > 0);
            left[sign] = inv2[bar];
            for (int i = s - 1; i >= 0 && std::abs(xs[i]) == 1; --i) {
                if (xs[i] < 0) sign = 1 - sign;
                if (i > 0) ++bar;
                left[sign] += inv2[bar];
            }
        }

        std::vector<mint> right(2, 0);
        {
            int sign = (xs[s] < 0), bar = (s < n - 1);
            right[sign] = inv2[bar];
            for (int i = s + 1; i < n && std::abs(xs[i]) == 1; ++i) {
                if (xs[i] < 0) sign = 1 - sign;
                if (i < n - 1) ++bar;
                right[sign] += inv2[bar];
            }
        }

        ans += left[0] * right[1] + left[1] * right[0];
    }

    std::cout << ans.val() << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0