結果

問題 No.3600 Moving Queen Many Times
コンテスト
ユーザー cho435
提出日時 2026-07-30 12:32:53
言語 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  
実行時間 545 ms / 7,000 ms
+ 650µs
コード長 2,410 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,764 ms
コンパイル使用メモリ 384,332 KB
実行使用メモリ 8,052 KB
最終ジャッジ日時 2026-07-30 12:33:06
合計ジャッジ時間 11,639 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 75
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)

template <class T> bool chmin(T& x, T y) {
	return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
	return x < y ? (x = y, true) : false;
}

using mint = atcoder::modint998244353;

void solve() {
	int h, w, sx, sy, gx, gy;
	ll K;
	cin >> h >> w >> sx >> sy >> gx >> gy >> K;
	sx--, sy--, gx--, gy--;

	vector<int> dx = {1, 1, 1, 0, -1, -1, -1, 0};
	vector<int> dy = {1, 0, -1, -1, -1, 0, 1, 1};
	auto isin = [&](int x, int y) {
		return 0 <= x && x < h && 0 <= y && y < w;
	};
	auto decode = [&](int x) {
		return pair(x / w, x % w);
	};
	auto encode = [&](int x, int y) {
		return x * w + y;
	};

	int n = h * w;

	int stid = encode(sx, sy);
	int glid = encode(gx, gy);

	vector pw(n, vector<mint>(n));
	vector<mint> sm(n, 0);
	rep(st, 0, n) {
		vector vv(n, vector<mint>(1 << n));
		vv[st][0] = 1;
		rep(bit, 0, 1 << n) rep(i, 0, n) if (vv[i][bit] != 0) {
			auto [x, y] = decode(i);
			rep(d, 0, 8) {
				int nx = x + dx[d], ny = y + dy[d];
				while (isin(nx, ny)) {
					int z = encode(nx, ny);
					if (!((bit >> z) & 1)) vv[z][bit | (1 << z)] += vv[i][bit];
					nx += dx[d], ny += dy[d];
				}
			}
		}
		rep(i, 0, n) pw[st][i] = vv[i].back();
		rep(bit, 0, 1 << n) if (popcount(1ULL * bit) == K % n) {
			sm[st] += vv[glid][bit];
		}
	}

	vector dp(n, vector<mint>(1 << n));
	dp[glid][1 << glid] = 1;
	rep(bit, 0, 1 << n) rep(i, 0, n) if (dp[i][bit] != 0) {
		auto [x, y] = decode(i);
		rep(d, 0, 8) {
			int nx = x + dx[d], ny = y + dy[d];
			while (isin(nx, ny)) {
				int z = encode(nx, ny);
				if (!((bit >> z) & 1)) dp[z][bit | (1 << z)] += dp[i][bit];
				nx += dx[d], ny += dy[d];
			}
		}
	}

	auto mul = [&](const vector<vector<mint>>& a,
				   const vector<vector<mint>>& b) {
		vector c(n, vector<mint>(n));
		rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) c[i][k] += a[i][j] * b[j][k];
		return c;
	};

	vector ar(n, vector<mint>(n));
	rep(i, 0, n) ar[i][i] = 1;

	K /= n;

	while (K) {
		if (K & 1) ar = mul(ar, pw);
		pw = mul(pw, pw);
		K /= 2;
	}

	mint ans = 0;
	rep(i, 0, n) ans += ar[stid][i] * sm[i];
	cout << ans.val() << '\n';
}

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