結果

問題 No.1907 DETERMINATION
ユーザー 👑 NachiaNachia
提出日時 2023-10-07 22:58:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 812 ms / 4,000 ms
コード長 7,329 bytes
コンパイル時間 1,320 ms
コンパイル使用メモリ 94,564 KB
実行使用メモリ 6,396 KB
最終ジャッジ日時 2023-10-07 22:59:32
合計ジャッジ時間 37,252 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 230 ms
4,892 KB
testcase_08 AC 95 ms
4,380 KB
testcase_09 AC 160 ms
4,380 KB
testcase_10 AC 582 ms
6,160 KB
testcase_11 AC 280 ms
4,808 KB
testcase_12 AC 553 ms
6,292 KB
testcase_13 AC 521 ms
5,876 KB
testcase_14 AC 504 ms
6,240 KB
testcase_15 AC 115 ms
4,376 KB
testcase_16 AC 36 ms
4,376 KB
testcase_17 AC 553 ms
6,344 KB
testcase_18 AC 397 ms
5,036 KB
testcase_19 AC 10 ms
4,384 KB
testcase_20 AC 542 ms
6,340 KB
testcase_21 AC 51 ms
4,376 KB
testcase_22 AC 537 ms
4,644 KB
testcase_23 AC 616 ms
5,908 KB
testcase_24 AC 173 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 559 ms
6,160 KB
testcase_27 AC 677 ms
6,084 KB
testcase_28 AC 705 ms
6,172 KB
testcase_29 AC 671 ms
6,140 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 568 ms
6,096 KB
testcase_32 AC 582 ms
6,304 KB
testcase_33 AC 555 ms
6,388 KB
testcase_34 AC 559 ms
6,156 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 592 ms
6,088 KB
testcase_39 AC 560 ms
6,088 KB
testcase_40 AC 812 ms
6,084 KB
testcase_41 AC 555 ms
6,312 KB
testcase_42 AC 805 ms
6,292 KB
testcase_43 AC 803 ms
6,236 KB
testcase_44 AC 631 ms
6,192 KB
testcase_45 AC 677 ms
6,244 KB
testcase_46 AC 585 ms
6,224 KB
testcase_47 AC 551 ms
6,196 KB
testcase_48 AC 573 ms
6,164 KB
testcase_49 AC 590 ms
6,396 KB
testcase_50 AC 584 ms
6,096 KB
testcase_51 AC 555 ms
6,156 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 671 ms
4,380 KB
testcase_54 AC 640 ms
4,380 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 671 ms
4,380 KB
testcase_57 AC 640 ms
4,376 KB
testcase_58 AC 556 ms
6,388 KB
testcase_59 AC 419 ms
6,220 KB
testcase_60 AC 419 ms
6,088 KB
testcase_61 AC 501 ms
6,164 KB
testcase_62 AC 420 ms
6,160 KB
testcase_63 AC 558 ms
6,172 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 #

#line 1 "..\\Main.cpp"

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
#line 2 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\linear-modulo\\matrix-modulo.hpp"

#line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\linear-modulo\\matrix-modulo.hpp"
#include <cassert>
#include <utility>


namespace nachia{

template<class Elem>
struct MatrixModulo{
private:
    int h;
    int w;
    std::vector<Elem> elems;

public:
    
    MatrixModulo(int new_h=0, int new_w=0){ h = new_h; w = new_w; elems.assign(h * w, 0); }
    MatrixModulo(const MatrixModulo &) = default;
    int numRow() const { return h; }
    int numColumn() const { return w; }
    int height() const { return numRow(); }
    int width() const { return numColumn(); }
    typename std::vector<Elem>::iterator operator[](int y){ return elems.begin() + (y * w); }
    typename std::vector<Elem>::const_iterator operator[](int y) const { return elems.begin() + (y * w); }
    static MatrixModulo Identity(int idx){ auto res = MatrixModulo(idx, idx); for(int i = 0; i < idx; i++) res[i][i] = 1; return res; }
    void swapColumns(int x1, int x2){
        assert(0 <= x1 && x1 < numColumn());
        assert(0 <= x2 && x2 < numColumn());
        for(int y=0; y<numRow(); y++) std::swap((*this)[y][x1], (*this)[y][x2]);
    }
    void swapRows(int y1, int y2){
        assert(0 <= y1 && y1 < numRow());
        assert(0 <= y2 && y2 < numRow());
        for(int x=0; x<numColumn(); x++) std::swap((*this)[y1][x], (*this)[y2][x]);
    }
    MatrixModulo operator*(const MatrixModulo& r) const {
        assert(width() == r.height());
        auto res = MatrixModulo(h, r.w);
        for (int i=0; i<h; i++) for (int j=0; j<w; j++) for (int k=0; k<r.w; k++) res[i][k] += (*this)[i][j] * r[j][k];
        return res;
    }
    Elem det() const {
        assert(height() == width());
        MatrixModulo g = *this;
        Elem ans = 1;
        for (int i=0; i<h; i++) {
            int tg = -1;
            for (int j=i; j<h; j++) { if (g[j][i].val() != 0) tg = j; }
            if (tg == -1) return 0;
            if (tg != i) ans = -ans;
            for (int j=0; j<h; j++) std::swap(g[i][j], g[tg][j]);
            tg = i;
            ans *= g[i][i];
            Elem const_coeff = g[i][i].inv();
            for (int j=0; j<h; j++) g[i][j] *= const_coeff;
            for (int j=i+1; j<h; j++) for(int k=h-1; k>=i; k--) g[j][k] -= g[j][i] * g[i][k];
        }
        return ans;
    }
    int rank() const {
        MatrixModulo g = *this;
        int y = 0;
        for (int d=0; d<w; d++) {
            if(y == h) break;
            int tg = -1;
            for (int i=y; i<h; i++) { if (g[i][d].val() != 0){ tg = i; break; } }
            if (tg == -1) continue;
            for (int j=d; j<w; j++) std::swap(g[y][j], g[tg][j]);
            tg = y;
            Elem const_coeff = g[y][d].inv();
            for (int j=d; j<w; j++) g[y][j] *= const_coeff;
            for (int i=y+1; i<h; i++) for(int j=w-1; j>=d; j--) g[i][j] -= g[i][d] * g[y][j];
            y++;
        }
        return y;
    }
    MatrixModulo pow(unsigned long long i){
        auto a = *this;
        auto res = Identity(height());
        while(i){
            if(i % 2 == 1) res = res * a;
            a = a * a;
            i /= 2;
        }
        return res;
    }
};


} // namespace nachia
#line 4 "D:\\Programming\\VSCode\\competitive-cpp\\nachia\\linear-modulo\\characteristic-polynomial.hpp"

namespace nachia{

template<class Elem>
std::vector<Elem> CharacteristicPolynomial(MatrixModulo<Elem> mat){
    assert(mat.numRow() == mat.numColumn());
    int n = mat.numRow();
    if(n == 0){ return {1}; }
    std::vector<Elem> T(n);
    for(int y=1; y<n; y++){
        int y1=y; while(y1<n && mat[y1][y-1].val() == 0) y1++;
        if(y1 == n) continue;
        if(y != y1){ mat.swapRows(y, y1); mat.swapColumns(y, y1); }
        T[y] = mat[y][y-1].inv();
        for(int y2=y+1; y2<n; y2++) T[y2] = T[y] * mat[y2][y-1];
        for(int y2=y+1; y2<n; y2++) for(int x=y-1; x<n; x++) mat[y2][x] -= mat[y][x] * T[y2];
        for(int y2=0; y2<n; y2++) for(int x=y+1; x<n; x++) mat[y2][y] += mat[y2][x] * T[x];
    }
    for(int y=0; y<n; y++){ Elem tmp = 1; for(int x=y+1; x<n; x++) mat[y][x] *= (tmp *= -mat[x][x-1]); }
    MatrixModulo<Elem> dp(n+1, n+1);
    dp[0][0] = 1;
    for(int y=0; y<n; y++){
        for(int x=0; x<=y; x++) dp[y+1][x+1] -= dp[y][x];
        for(int x=0; x<=y; x++) dp[y+1][x] += dp[y][x] * mat[y][y];
        for(int y2=0; y2<y; y2++) for(int x=0; x<=y2; x++) dp[y+1][x] += dp[y2][x] * mat[y2][y];
    }
    std::vector<Elem> res(n+1);
    for(int i=0; i<=n; i++) res[i] = ((n%2 == 1) ? -dp[n][i] : dp[n][i]);
    return res;
}

} // namespace nachia
#line 8 "..\\Main.cpp"
using Modint = atcoder::static_modint<998244353>;
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
const i64 INF = 1001001001001001001;

struct ModintX{
    Modint x;
    Modint c;
    ModintX& operator+=(ModintX a){ x += a.x; c += a.c; return *this; }
    ModintX& operator-=(ModintX a){ x -= a.x; c -= a.c; return *this; }
    ModintX& operator*=(Modint a){ x *= a; c *= a; return *this; }
    ModintX operator*(Modint a) const { return { x*a, c*a }; }
    void sh(){ x = c; c = 0; }
};

void testcase(){
    int n; cin >> n;
    vector<vector<ModintX>> Mt(n, vector<ModintX>(n));
    rep(i,n) rep(j,n){
        int x; cin >> x;
        Mt[i][j].c = x;
    }
    rep(i,n) rep(j,n){
        int x; cin >> x;
        Mt[i][j].x = x;
    }
    int shifted = 0;
    Modint times = 1;
    rep(y,n){
        if(shifted == n+1){
            rep(i,n+1) cout << "0\n";
            return;
        }
        int p = y;
        while(p < n && Mt[p][y].x.val() == 0) p++;
        if(p == n){
            shifted++;
            rep(q,n) Mt[q][y].sh();
            p = y;
            vector<Modint> t(y);
            rep(y2,y) t[y2] = -Mt[y2][y].x;
            rep(y2,n) rep(x,y) Mt[y2][y] += Mt[y2][x] * t[x];
            y--; continue;
        }
        swap(Mt[p], Mt[y]); if(p != y) times = -times;
        {
            Modint t = Mt[y][y].x;
            times *= t; t = t.inv();
            rep(i,n) Mt[y][i] *= t;
        }
        for(int y2=y+1; y2<n; y2++){
            Modint t = -Mt[y2][y].x;
            rep(x,n) Mt[y2][x] += Mt[y][x] * t;
        }
        {
            vector<Modint> t(n);
            for(int x=y+1; x<n; x++) t[x] = -Mt[y][x].x;
            rep(y2,n) for(int x=y+1; x<n; x++) Mt[y2][x] += Mt[y2][y] * t[x];
        }
        rep(i,n) rep(j,n){
            if(i > y && j > y) continue;
            if(i == j) assert(Mt[i][j].x.val() == 1);
            else assert(Mt[i][j].x.val() == 0);
        }
    }
    rep(i,n) rep(j,n){
        if(i == j) assert(Mt[i][j].x.val() == 1);
        else assert(Mt[i][j].x.val() == 0);
    }
    using Matrix = nachia::MatrixModulo<Modint>;
    auto matc = Matrix(n,n);
    rep(i,n) rep(j,n) matc[i][j] = -Mt[i][j].c;
    auto poly = nachia::CharacteristicPolynomial(matc);
    rep(i,n+1) poly[i] *= times;
    poly.resize(n*2+2);
    rep(i,n+1) cout << poly[i+shifted].val() << '\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    testcase();
    return 0;
}
0