結果
問題 | No.2825 Sum of Scores of Sets of Specified Sections |
ユーザー | rniya |
提出日時 | 2024-07-26 23:39:13 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 148 ms / 3,000 ms |
コード長 | 4,872 bytes |
コンパイル時間 | 3,681 ms |
コンパイル使用メモリ | 265,176 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-26 23:39:20 |
合計ジャッジ時間 | 6,067 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 32 |
ソースコード
#include <bits/stdc++.h> #ifdef LOCAL #include <debug.hpp> #else #define debug(...) void(0) #endif template <class T> std::istream& operator>>(std::istream& is, std::vector<T>& v) { for (auto& e : v) { is >> e; } return is; } template <class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) { for (std::string_view sep = ""; const auto& e : v) { os << std::exchange(sep, " ") << e; } return os; } template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = std::forward<U>(y), true); } template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = std::forward<U>(y), true); } template <class T> void mkuni(std::vector<T>& v) { std::ranges::sort(v); auto result = std::ranges::unique(v); v.erase(result.begin(), result.end()); } template <class T> int lwb(const std::vector<T>& v, const T& x) { return std::distance(v.begin(), std::ranges::lower_bound(v, x)); } #include <atcoder/modint> template <typename T> std::vector<T> characteristic_polynomial(std::vector<std::vector<T>> M) { assert(M.empty() or M.size() == M[0].size()); int n = M.size(); // reduce M to upper Hessenberg form for (int j = 0; j < n - 2; j++) { for (int i = j + 2; i < n; i++) { if (M[i][j] != 0) { std::swap(M[j + 1], M[i]); for (int k = 0; k < n; k++) std::swap(M[k][j + 1], M[k][i]); break; } } if (M[j + 1][j] == 0) continue; auto inv = T(1) / M[j + 1][j]; for (int i = j + 2; i < n; i++) { auto coef = M[i][j] * inv; for (int k = j; k < n; k++) M[i][k] -= coef * M[j + 1][k]; for (int k = 0; k < n; k++) M[k][j + 1] += coef * M[k][i]; } } // compute the characteristic polynomial of upper Hessenberg matrix M std::vector<std::vector<T>> p(n + 1); p[0] = {T(1)}; for (int i = 0; i < n; i++) { p[i + 1].resize(i + 2); for (int j = 0; j <= i; j++) { p[i + 1][j + 1] += p[i][j]; p[i + 1][j] -= p[i][j] * M[i][i]; } T betas = 1; for (int j = i - 1; j >= 0; j--) { betas *= M[j + 1][j]; T coef = -betas * M[j][i]; for (int k = 0; k <= j; k++) p[i + 1][k] += coef * p[j][k]; } } return p[n]; } template <typename T> std::vector<T> determinant_polynomial(std::vector<std::vector<T>> M0, std::vector<std::vector<T>> M1) { assert(M0.size() == M1.size()); assert(M0.size() == M0[0].size()); assert(M1.size() == M1[0].size()); int n = M0.size(), off = 0; T prod = 1; for (int p = 0; p < n; p++) { int pivot = -1; for (int i = p; i < n; i++) { if (M1[i][p] != 0) { pivot = i; break; } } if (pivot == -1) { if (++off > n) return std::vector<T>(n + 1, 0); for (int i = 0; i < p; i++) { for (int k = 0; k < n; k++) M0[k][p] -= M1[i][p] * M0[k][i]; M1[i][p] = 0; } for (int i = 0; i < n; i++) std::swap(M0[i][p], M1[i][p]); p--; continue; } if (pivot != p) { std::swap(M0[p], M0[pivot]); std::swap(M1[p], M1[pivot]); prod *= -1; } prod *= M1[p][p]; auto inv = T(1) / M1[p][p]; for (int j = 0; j < n; j++) { M0[p][j] *= inv; M1[p][j] *= inv; } for (int i = 0; i < n; i++) { if (i == p) continue; auto coef = M1[i][p]; for (int j = 0; j < n; j++) { M0[i][j] -= M0[p][j] * coef; M1[i][j] -= M1[p][j] * coef; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { M0[i][j] *= -1; } } auto poly = characteristic_polynomial(M0); std::vector<T> res(n + 1, 0); for (int i = 0; i + off <= n; i++) res[i] = prod * poly[i + off]; return res; } using ll = long long; using namespace std; using mint = atcoder::modint998244353; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); int H, W; cin >> H >> W; vector A(H, vector<int>(W)), B(H, vector<int>(W)); cin >> A >> B; vector C(H, vector<mint>(H, 0)), D(H, vector<mint>(H, 0)); for (int i = 0; i < H; i++) { D[i][i] = 1; for (int j = 0; j < H; j++) { for (int k = 0; k < W; k++) { C[i][j] += mint(A[i][k]) * B[j][k]; } } } auto res = determinant_polynomial(C, D); mint ans = accumulate(res.begin(), res.end(), mint(0)) - 1; cout << ans.val() << "\n"; return 0; }