結果

問題 No.1907 DETERMINATION
ユーザー SumitacchanSumitacchan
提出日時 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
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#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/68640
namespace 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 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];
}
} // namespace LibraryChecker
mmat 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;
// ddB1
int d = 0;
while(d < N){
// M1d0d0
int i0 = -1;
FOR(i, d, N) if(M1[i][d].val() != 0){
i0 = i;
break;
}
if(i0 == -1){
// M1d0dx
f1++;
if(f1 > N){
// det(M0+xM1)Nx^(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]);
}
// dx
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);
// ddx
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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0