結果
問題 | No.1907 DETERMINATION |
ユーザー |
![]() |
提出日時 | 2022-02-06 17:41:03 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,116 ms / 4,000 ms |
コード長 | 5,889 bytes |
コンパイル時間 | 2,622 ms |
コンパイル使用メモリ | 210,136 KB |
最終ジャッジ日時 | 2025-01-27 20:36:01 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 63 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/modint>using namespace std;using namespace atcoder;struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)#define REP(i, n) FOR(i,0,n)#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)#define IREP(i, n) IFOR(i,0,n)using ll = long long;const int mod = 998244353;using mint = modint998244353;using mvec = vector<mint>;using mmat = vector<mvec>;#define debug(x) cout << #x << "=" << x << endl;#define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, (int)v.size()){ cout << v[i_debug] << ","; } cout << endl; }#define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, (int)m.size()){ REP(j_debug, (int)m[i_debug].size()){ cout << m[i_debug][j_debug] << ",";} cout << endl;} }// Library Checker "Characteristic Polynomial" より引用// https://judge.yosupo.jp/submission/68640namespace LibraryChecker {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 formfor (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 Mstd::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];}} // namespace LibraryCheckermmat read_matrix(int N){mmat M(N, mvec(N));REP(i, N) REP(j, N){int x; cin >> x;M[i][j] = x;}return M;}void write_matrix(mmat M){int N = M.size();REP(i, N){REP(j, N) cout << M[i][j].val() << ',';cout << endl;}}//swap M[i,:] and M[j,:]void swap_row(mmat &M, int i, int j){assert(i != j);M[i].swap(M[j]);}//swap M[:,i] and M[:,j]void swap_column(mmat &M, int i, int j){assert(i != j);int N = M.size();REP(k, N) swap(M[k][i], M[k][j]);}//M[i,:]-=a*M[j:]void sbt_row(mmat &M, int i, int j, mint a){assert(i != j);int N = M.size();REP(k, N) M[i][k] -= a * M[j][k];}//M[:,i]-=a*M[:,j]void sbt_column(mmat &M, int i, int j, mint a){assert(i != j);int N = M.size();REP(k, N) M[k][i] -= a * M[k][j];}//M[i,:]*=a;void mult_row(mmat &M, int i, mint a){int N = M.size();REP(k, N) M[i][k] *= a;}mvec solve(mmat M0, mmat M1){int N = M0.size();// 最後にf0*x^{-f1}を行列式にかけるmint f0 = 1;int f1 = 0;// 上d行と左d列では、Bの要素は対角上の1のみになるようにしていくint d = 0;while(d < N){// M1のd列目から非0要素を探す、上d行は0なので探さなくてよいint i0 = -1;FOR(i, d, N) if(M1[i][d].val() != 0){i0 = i;break;}if(i0 == -1){// M1のd列目が全て0ならば、d列目にxをかけるf1++;if(f1 > N){// det(M0+xM1)は高々N次式なので、x^(N+1)で割れるのは0の場合のみreturn mvec(N + 1, 0);}FOR(i, 0, N){assert(M1[i][d].val() == 0);swap(M0[i][d], M1[i][d]);}// 上d行のxは消すFOR(i, 0, d){assert(M1[i][i].val() == 1);mint a = M1[i][d];sbt_column(M0, d, i, a);sbt_column(M1, d, i, a);}// やり直す、f1に上限があるため無限ループにはならないcontinue;}if(i0 != d){// 行交換f0 *= -1;swap_row(M0, i0, d);swap_row(M1, i0, d);}// M1[d][d]=1にするf0 *= M1[d][d];mint r = (mint)1 / M1[d][d];mult_row(M0, d, r);mult_row(M1, d, r);assert(M1[d][d].val() == 1);// d行目とd列目から他のx消去FOR(i, d + 1, N){mint a = M1[i][d];sbt_row(M0, i, d, a);sbt_row(M1, i, d, a);}FOR(j, d + 1, N){mint a = M1[d][j];sbt_column(M0, j, d, a);sbt_column(M1, j, d, a);}d++;}// M1は単位行列になっているはずREP(i, N) REP(j, N){assert(M1[i][j].val() == (i == j));}// 答えは det(xI-(-M0))*(f0*x^{-f1})REP(i, N) REP(j, N) M0[i][j] *= -1;auto cp = LibraryChecker::characteristic_polynomial(M0);mvec ans(N + 1, 0);FOR(i, 0, f1) assert(cp[i].val() == 0);FOR(i, f1, N + 1) ans[i - f1] = cp[i] * f0;return ans;}int main(){int N; cin >> N;auto M0 = read_matrix(N);auto M1 = read_matrix(N);auto ans = solve(M0, M1);REP(i, N + 1) cout << ans[i].val() << endl;return 0;}