結果

問題 No.3600 Moving Queen Many Times
コンテスト
ユーザー K2
提出日時 2026-07-24 23:52:12
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 750 ms / 7,000 ms
+ 997µs
コード長 2,844 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,452 ms
コンパイル使用メモリ 418,544 KB
実行使用メモリ 116,736 KB
最終ジャッジ日時 2026-07-24 23:52:27
合計ジャッジ時間 14,985 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 75
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#ifndef DEBUG
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("tune=native")
#endif

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

using ll = long long;
using mint = modint998244353;
// using mint = modint1000000007;

template <typename T> using vec = vector<T>;
template <typename T> using pr = pair<T, T>;
template <typename T> using mipq = priority_queue<T, vec<T>, greater<T>>;

#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(i, n) for (auto i = decay_t<decltype(n)>{}; i < (n); i++)
#define rep2(i, l, r) for (auto i = (l); i < (r); i++)
#define rep3(i, l, r, d) for (auto i = (l); i < (r); i += (d))
#define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(i, r, l) for (auto i = (r); i >= (l); i--)
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; }
template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; }

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;

void solve();

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}

vec<vec<mint>> mat_mul(const vec<vec<mint>>& a, const vec<vec<mint>>& b) {
    int n = a.size();
    vec res(n, vec<mint>(n));
    rep(i, n) rep(k, n) rep(j, n) res[i][j] += a[i][k] * b[k][j];
    return res;
}

vec<vec<mint>> mat_pow(vec<vec<mint>> a, ll k) {
    assert(0 <= k);
    int n = a.size();
    vec res(n, vec<mint>(n));
    rep(i, n) res[i][i] = 1;
    while (k) {
        if (k & 1) res = mat_mul(res, a);
        a = mat_mul(a, a);
        k >>= 1;
    }
    return res;
}

void solve() {
    int H, W, SX, SY, GX, GY; ll K;
    cin >> H >> W >> SX >> SY >> GX >> GY >> K;
    SX--; SY--; GX--; GY--;

    int S = SX * W + SY, G = GX * W + GY, L = H * W;

    auto moveable = [&] (int s, int t) {
        int si = s / W, sj = s % W, ti = t / W, tj = t % W;
        return s != t and (si == ti or sj == tj or si + sj == ti + tj or si - sj == ti - tj);
    };

    vec dp(L, vec(1 << L, vec<mint>(L)));
    vec mat(L, vec<mint>(L));
    rep(s, L) {
        rep(i, L) if (moveable(s, i)) dp[s][1 << i][i] = 1;
        rep(bit, 1 << L) rep(i, L) if (bit >> i & 1) {
            if (dp[s][bit][i] == 0) continue;
            rep(j, L) if (not (bit >> j & 1) and moveable(i, j)) {
                dp[s][bit | 1 << j][j] += dp[s][bit][i];
            }
        }
        rep(i, L) mat[s][i] = dp[s][(1 << L) - 1][i];
    }

    mat = mat_pow(mat, K / L);
    K %= L;

    mint res = 0;
    if (K) {
        rep(i, L) rep(bit, 1U << L) if (bit >> G & 1 and popcount(bit) == K) {
            res += mat[S][i] * dp[i][bit][G];
        }
    } else {
        res = mat[S][G];
    }

    cout << res.val() << '\n';
}
0