結果

問題 No.2159 Filling 4x4 array
ユーザー suisensuisen
提出日時 2022-12-22 22:24:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4,025 ms / 5,000 ms
コード長 2,974 bytes
コンパイル時間 1,123 ms
コンパイル使用メモリ 95,992 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-11 11:28:15
合計ジャッジ時間 79,217 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 2,752 ms
4,380 KB
testcase_05 AC 6 ms
4,384 KB
testcase_06 AC 15 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 17 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 9 ms
4,384 KB
testcase_12 AC 6 ms
4,380 KB
testcase_13 AC 14 ms
4,380 KB
testcase_14 AC 20 ms
4,376 KB
testcase_15 AC 11 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 8 ms
4,380 KB
testcase_18 AC 7 ms
4,376 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 29 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 14 ms
4,380 KB
testcase_24 AC 9 ms
4,376 KB
testcase_25 AC 3,529 ms
4,380 KB
testcase_26 AC 3,810 ms
4,380 KB
testcase_27 AC 4,025 ms
4,376 KB
testcase_28 AC 3,754 ms
4,380 KB
testcase_29 AC 3,760 ms
4,376 KB
testcase_30 AC 3,969 ms
4,376 KB
testcase_31 AC 3,719 ms
4,380 KB
testcase_32 AC 3,220 ms
4,380 KB
testcase_33 AC 3,893 ms
4,376 KB
testcase_34 AC 3,672 ms
4,380 KB
testcase_35 AC 3,419 ms
4,380 KB
testcase_36 AC 3,141 ms
4,380 KB
testcase_37 AC 3,328 ms
4,380 KB
testcase_38 AC 3,746 ms
4,380 KB
testcase_39 AC 3,119 ms
4,384 KB
testcase_40 AC 3,671 ms
4,380 KB
testcase_41 AC 3,640 ms
4,388 KB
testcase_42 AC 3,763 ms
4,376 KB
testcase_43 AC 3,646 ms
4,384 KB
testcase_44 AC 3,766 ms
4,376 KB
testcase_45 AC 85 ms
4,380 KB
testcase_46 AC 5 ms
4,384 KB
testcase_47 AC 167 ms
4,380 KB
testcase_48 AC 4 ms
4,380 KB
testcase_49 AC 193 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <iostream>
#include <unordered_map>

#include <atcoder/modint>

using mint = atcoder::modint998244353;

constexpr int L = 30;
constexpr int N = 4;

int main() {
    std::array<int, N> r, c;
    for (auto&& e : r) std::cin >> e, e -= 4;
    for (auto&& e : c) std::cin >> e, e -= 4;

    constexpr int K = 3;

    using key = uint32_t;
    using state = std::pair<std::array<int, N>, std::array<int, N>>;

    auto get_state = [&](key k, int bit) {
        auto bit_slice = [&](int i) {
            return (k >> (i * K)) & ((1 << K) - 1);
        };
        state s;
        auto &[sr, sc] = s;
        for (int a = 0; a < N; ++a) {
            sr[a] = (r[a] >> bit) - bit_slice(a);
        }
        for (int b = 0; b < N; ++b) {
            sc[b] = (c[b] >> bit) - bit_slice(N + b);
        }
        return s;
    };
    auto get_key = [&](const state& s, int bit) {
        const auto &[sr, sc] = s;
        key k = 0;
        auto add = [&](int v, int i) {
            k |= v << (i * K);
        };

        for (int a = 0; a < N; ++a) {
            add((r[a] >> bit) - sr[a], a);
        }
        for (int b = 0; b < N; ++b) {
            add((c[b] >> bit) - sc[b], N + b);
        }
        return k;
    };

    std::unordered_map<key, mint> pd{ { 0, 1 } };
    for (int i = 0; i < L; ++i) {
        std::unordered_map<key, mint> dp;
        for (const auto& [k, v] : pd) {
            for (int s = 0; s < 1 << ((N - 1) * (N - 1)); ++s) {
                auto [r, c] = get_state(k, i);
                std::array<std::array<int, N>, N> d;
                for (int a = 0; a < N - 1; ++a) for (int b = 0; b < N - 1; ++b) {
                    int bit = (s >> (a * (N - 1) + b)) & 1;
                    d[a][b] = bit;
                    r[a] -= bit, c[b] -= bit;
                }
                for (int a = 0; a < N - 1; ++a) {
                    const int b = N - 1;
                    int bit = r[a] & 1;
                    d[a][b] = bit;
                    r[a] -= bit, c[b] -= bit;
                }
                for (int b = 0; b < N - 1; ++b) {
                    const int a = N - 1;
                    int bit = c[b] & 1;
                    d[a][b] = bit;
                    r[a] -= bit, c[b] -= bit;
                }
                {
                    int bit = r[N - 1] & 1;
                    d[N - 1][N - 1] = bit;
                    r[N - 1] -= bit, c[N - 1] -= bit;
                }
                bool ok = (c[N - 1] & 1) == 0;
                for (int a = 0; a < N; ++a) {
                    ok &= r[a] >= 0;
                    r[a] >>= 1;
                }
                for (int b = 0; b < N; ++b) {
                    ok &= c[b] >= 0;
                    c[b] >>= 1;
                }
                if (ok) {
                    dp[get_key({ r, c }, i + 1)] += v;
                }
            }
        }
        pd.swap(dp);
    }

    std::cout << pd[0].val() << std::endl;

    return 0;
}
0