結果
問題 | No.2159 Filling 4x4 array |
ユーザー |
|
提出日時 | 2022-12-22 22:24:30 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4,061 ms / 5,000 ms |
コード長 | 2,974 bytes |
コンパイル時間 | 2,717 ms |
コンパイル使用メモリ | 116,516 KB |
最終ジャッジ日時 | 2025-02-09 18:31:47 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 45 |
ソースコード
#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;}