結果

問題 No.3635 Probability trip
コンテスト
ユーザー kwm_t
提出日時 2026-08-21 22:14:10
言語 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  
実行時間 54 ms / 2,000 ms
+ 230µs
コード長 3,596 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,288 ms
コンパイル使用メモリ 381,816 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-08-21 22:14:19
合計ジャッジ時間 7,871 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
// using mint = modint1000000007;
// const int mod = 1000000007;
using mint = modint998244353;
const int mod = 998244353;
// const int INF = 1e9;
// const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, l, r) for (int i = (l); i < (r); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define P pair<int, int>
template<typename A, typename B> inline bool chmax(A& a, const B& b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A& a, const B& b) { if (a > b) { a = b; return true; } return false; }
#ifndef KWM_T_MATH_MATRIX_SIMPLE_MATRIX_HPP
#define KWM_T_MATH_MATRIX_SIMPLE_MATRIX_HPP

// #include
#include <vector>
#include <cassert>

/**
 * @brief シンプルな行列演算(vector版・軽量)
 *
 * 最低限の行列演算(乗算・累乗)を提供する。
 * とりあえず使う用の軽量実装。
 *
 * 典型用途:
 *   - 行列累乗(DP遷移)
 *   - 小規模な行列計算
 *
 * 計算量:
 *   - 乗算: O(N^3)
 *   - 累乗: O(N^3 log K)
 *
 * @tparam T
 *   - 要素型(+, *, += が定義されていること)
 *
 * 制約 / 注意:
 *   - 正方行列のみ想定(matrixPow)
 *   - サイズ不一致は assert
 *   - 単位元は T(1) を使用
 *
 * 使用例:
 *   vector<vector<long long>> A(n, vector<long long>(n));
 *   auto B = matrixPow(10, A);
 *
 * verified:
 *   - https://atcoder.jp/contests/awc0053/submissions/75169363
 */
namespace kwm_t::math::matrix {

template<typename T>
std::vector<std::vector<T>> matrixMul(
	const std::vector<std::vector<T>>& A,
	const std::vector<std::vector<T>>& B
) {
	assert(!A.empty() && !B.empty());
	assert(A[0].size() == B.size());

	int n = (int)A.size();
	int m = (int)B[0].size();
	int p = (int)A[0].size();

	std::vector<std::vector<T>> C(n, std::vector<T>(m, T(0)));

	for (int i = 0; i < n; ++i) {
		for (int k = 0; k < p; ++k) {
			for (int j = 0; j < m; ++j) {
				C[i][j] += A[i][k] * B[k][j];
			}
		}
	}
	return C;
}

template<typename T>
std::vector<std::vector<T>> matrixPow(
	long long n,
	const std::vector<std::vector<T>>& mat
) {
	assert(!mat.empty());
	assert(mat.size() == mat[0].size());

	int size = (int)mat.size();

	std::vector<std::vector<T>> res(size, std::vector<T>(size, T(0)));
	for (int i = 0; i < size; ++i) {
		res[i][i] = T(1);
	}

	auto base = mat;

	while (n > 0) {
		if (n & 1) res = matrixMul(res, base);
		base = matrixMul(base, base);
		n >>= 1;
	}

	return res;
}

} // namespace kwm_t::math::matrix

#endif // KWM_T_MATH_MATRIX_SIMPLE_MATRIX_HPP
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n, m; cin >> n >> m;
	vector g(n, vector<int>());
	rep(i, m) {
		int u, v; cin >> u >> v;
		u--, v--;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	vector mat(n, vector<mint>(n));
	rep(i, n) {
		mint inv = ((mint)g[i].size()).inv();
		for (auto j : g[i])mat[i][j] = inv;
	}
	mint p, q;
	long long s, t; cin >> s >> t;
	int a, b; cin >> a >> b;
	a--, b--;
	{
		// s日後,0->a
		auto ms = kwm_t::math::matrix::matrixPow(s - 1, mat);
		p = ms[0][a];
	}
	{
		// t日後,0->b
		auto mt1 = kwm_t::math::matrix::matrixPow(t - 1, mat);
		q = mt1[0][b];
		// s-t日後,b->a
		auto mt2 = kwm_t::math::matrix::matrixPow(s - t, mat);
		q *= mt2[b][a];
	}
	mint ans = q / p;
	cout << ans.val() << endl;
	return 0;
}
0