結果

問題 No.1907 DETERMINATION
ユーザー SumitacchanSumitacchan
提出日時 2022-02-06 18:08:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 588 ms / 4,000 ms
コード長 5,910 bytes
コンパイル時間 2,865 ms
コンパイル使用メモリ 214,908 KB
実行使用メモリ 7,792 KB
最終ジャッジ日時 2023-08-26 00:10:51
合計ジャッジ時間 29,127 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 235 ms
5,696 KB
testcase_08 AC 92 ms
4,704 KB
testcase_09 AC 162 ms
5,340 KB
testcase_10 AC 508 ms
7,360 KB
testcase_11 AC 248 ms
6,304 KB
testcase_12 AC 560 ms
7,448 KB
testcase_13 AC 528 ms
7,360 KB
testcase_14 AC 495 ms
7,504 KB
testcase_15 AC 113 ms
5,004 KB
testcase_16 AC 32 ms
4,380 KB
testcase_17 AC 471 ms
7,380 KB
testcase_18 AC 340 ms
6,340 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 517 ms
7,348 KB
testcase_21 AC 46 ms
4,380 KB
testcase_22 AC 385 ms
6,224 KB
testcase_23 AC 526 ms
7,384 KB
testcase_24 AC 159 ms
5,172 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 582 ms
7,572 KB
testcase_27 AC 581 ms
7,580 KB
testcase_28 AC 575 ms
7,708 KB
testcase_29 AC 581 ms
7,484 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 585 ms
7,508 KB
testcase_32 AC 579 ms
7,488 KB
testcase_33 AC 582 ms
7,512 KB
testcase_34 AC 584 ms
7,492 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 585 ms
7,756 KB
testcase_39 AC 580 ms
7,612 KB
testcase_40 AC 586 ms
7,648 KB
testcase_41 AC 582 ms
7,668 KB
testcase_42 AC 582 ms
7,792 KB
testcase_43 AC 587 ms
7,612 KB
testcase_44 AC 580 ms
7,492 KB
testcase_45 AC 580 ms
7,644 KB
testcase_46 AC 575 ms
7,512 KB
testcase_47 AC 582 ms
7,488 KB
testcase_48 AC 588 ms
7,732 KB
testcase_49 AC 579 ms
7,588 KB
testcase_50 AC 586 ms
7,504 KB
testcase_51 AC 585 ms
7,488 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 409 ms
6,192 KB
testcase_54 AC 410 ms
6,264 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 409 ms
6,160 KB
testcase_57 AC 408 ms
6,264 KB
testcase_58 AC 418 ms
7,608 KB
testcase_59 AC 429 ms
7,604 KB
testcase_60 AC 434 ms
7,564 KB
testcase_61 AC 479 ms
7,576 KB
testcase_62 AC 432 ms
7,572 KB
testcase_63 AC 584 ms
7,528 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 2 ms
4,376 KB
testcase_66 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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を行列式にかける
    mint f0 = 1;

    // 上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は正則でない
            return mvec(N + 1, 0);
        }

        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
    REP(i, N) REP(j, N) M0[i][j] *= -1;
    auto cp = LibraryChecker::characteristic_polynomial(M0);
    mvec ans(N + 1);
    REP(i, N + 1) ans[i] = cp[i] * f0;
    return ans;
}

int main(){

    int N; cin >> N;
    auto M0 = read_matrix(N);
    auto M1 = read_matrix(N);

    mint a = 63504649;
    // x=y-a とする
    // det(M0+xM1)=det((M0-aM1)+yM1)=y^N det((M0-aM1)y^(-1)+M1)
    // M0+xM1が正則ならばM0-aM1は高い確率で正則
    
    // det((M0-aM1)y^(-1)+M1)をy^(-1)の多項式として求める
    mmat M0_2 = M0;
    REP(i, N) REP(j, N) M0_2[i][j] -= a * M1[i][j];
    auto poly_y = solve(M1, M0_2);
    // y^N を乗じ、yの多項式に変換
    REP(i, N + 1) if(i < N - i) swap(poly_y[i], poly_y[N - i]);

    // y=x+aを代入して答えを得る
    // 二項係数
    mmat ncr(N + 1, mvec(N + 1));
    REP(i, N + 1){
        ncr[i][0] = ncr[i][i] = 1;
        FOR(j, 1, i) ncr[i][j] = ncr[i - 1][j - 1] + ncr[i - 1][j];
    }
    mvec poly_x(N + 1);
    REP(i, N + 1) REP(j, i + 1){
        poly_x[j] += poly_y[i] * ncr[i][j] * a.pow(i - j);
    }

    REP(i, N + 1) cout << poly_x[i].val() << endl;

    return 0;
}
0