結果

問題 No.1907 DETERMINATION
ユーザー SumitacchanSumitacchan
提出日時 2022-02-05 19:36:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,822 bytes
コンパイル時間 3,778 ms
コンパイル使用メモリ 220,424 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-06-06 19:02:21
合計ジャッジ時間 26,369 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 220 ms
5,760 KB
testcase_08 AC 88 ms
5,376 KB
testcase_09 AC 149 ms
5,376 KB
testcase_10 AC 471 ms
6,144 KB
testcase_11 AC 100 ms
5,760 KB
testcase_12 AC 527 ms
7,424 KB
testcase_13 AC 504 ms
7,040 KB
testcase_14 AC 471 ms
7,040 KB
testcase_15 AC 108 ms
5,376 KB
testcase_16 AC 31 ms
5,376 KB
testcase_17 AC 456 ms
6,016 KB
testcase_18 AC 314 ms
5,888 KB
testcase_19 AC 10 ms
5,376 KB
testcase_20 AC 500 ms
6,912 KB
testcase_21 AC 47 ms
5,376 KB
testcase_22 AC 272 ms
5,632 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 544 ms
7,680 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 2 ms
5,376 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 543 ms
7,680 KB
testcase_34 AC 545 ms
7,552 KB
testcase_35 WA -
testcase_36 AC 2 ms
5,376 KB
testcase_37 WA -
testcase_38 AC 539 ms
7,552 KB
testcase_39 AC 537 ms
7,552 KB
testcase_40 WA -
testcase_41 AC 539 ms
7,552 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 531 ms
7,424 KB
testcase_47 AC 547 ms
7,552 KB
testcase_48 AC 543 ms
7,552 KB
testcase_49 AC 527 ms
7,296 KB
testcase_50 AC 534 ms
7,680 KB
testcase_51 AC 550 ms
7,552 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 384 ms
5,760 KB
testcase_54 AC 390 ms
5,760 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 394 ms
5,632 KB
testcase_57 AC 387 ms
5,632 KB
testcase_58 AC 320 ms
5,888 KB
testcase_59 AC 404 ms
7,680 KB
testcase_60 AC 407 ms
7,552 KB
testcase_61 AC 467 ms
7,040 KB
testcase_62 AC 404 ms
7,552 KB
testcase_63 AC 546 ms
7,552 KB
testcase_64 AC 2 ms
5,376 KB
testcase_65 AC 2 ms
5,376 KB
testcase_66 AC 2 ms
5,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;
    }
}

mmat mult_matrix(mmat A, mmat B){
    int N = A.size();
    mmat C(N, mvec(N));
    REP(i, N) REP(j, N) REP(k, N) C[i][j] += A[i][k] * B[k][j];
    return C;
}

//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;
}

template <typename T>
T determinant(vector<vector<T>> A){
    int N = A.size();
    for(int j = 0; j < N; j++) assert((int)A[j].size() == N);
    T m1 = 0; m1 -= 1; // -1, mintでバグらないように

    T d = 1;
    for(int j = 0; j < N; j++){
        int i0 = -1;
        for(int i = j; i < N; i++) if(!(A[i][j] == 0)){
            i0 = i;
            break;
        }
        if(i0 == -1) return 0;
        if(i0 != j){
            d *= m1;
            A[j].swap(A[i0]);
        }
        d *= A[j][j];
        for(int i = j + 1; i < N; i++){
            if(A[i][j] == 0) continue;
            T alpha = A[i][j] / A[j][j];
            for(int k = j; k < N; k++){
                A[i][k] -= A[j][k] * alpha;
            }
        }
    }
    return d;
}

struct fast_random
{
    uint32_t x;

    fast_random(int seed = 2463534242): x(seed){}

    uint32_t xorshift(){
        x ^= (x << 13);
        x ^= (x >> 17);
        x ^= (x << 5);
        return x;
    }

    //random integer in [a, b]
    uint32_t randint(uint32_t a, uint32_t b){
        return a + xorshift() % (b - a + 1);
    }

    double uniform(){
        return (double)xorshift() / UINT32_MAX;
    }

    double uniform(double a, double b){
        return a + uniform() * (b - a);
    }

};
fast_random rng;

//return a random matrix with determinant 1
mmat random_matrix(int N){
    while(true){
        mmat A(N, mvec(N));
        REP(i, N) REP(j, N) A[i][j] = rng.randint(0, mod - 1);
        mint D = determinant(A);
        if(D.val() != 0){
            REP(j, N) A[0][j] /= D;
            return A;
        }
    }
}

bool solve(mmat A, mmat B, mvec &ans){
    /*

    X *
    O D
    
    の形にする
    ここで、X=xI-M (Mは定数行列), Dは定数のみの上三角行列
    Xのサイズはrank(B)
    Dは正則でなければならない
    */

    int N = A.size();

    //まずX求める
    int rankB = 0;
    mint factor = 1;
    REP(d, N){
        int x = -1, y = -1;
        FOR(i, d, N){
            FOR(j, d, N){
                if(B[i][j].val() != 0){
                    x = i; y = j;
                    break;
                }
            }
            if(x != -1) break;
        }
        if(x == -1) break;

        rankB++;
        if(x != d){
            swap_row(A, x, d);
            swap_row(B, x, d);
            factor *= -1;
        }
        if(y != d){
            swap_column(A, y, d);
            swap_column(B, y, d);
            factor *= -1;
        }
        FOR(i, d + 1, N){
            mint a = B[i][d] / B[d][d];
            sbt_row(A, i, d, a);
            sbt_row(B, i, d, a);
        }
        FOR(j, d + 1, N){
            mint a = B[d][j] / B[d][d];
            sbt_column(A, j, d, a);
            sbt_column(B, j, d, a);
        }
        factor *= B[d][d];
        mult_row(A, d, (mint)1 / B[d][d]);
        B[d][d] = 1;
    }
    // 右下整理してDを求める
    FOR(d, rankB, N){
        int x = -1, y = -1;
        FOR(i, d, N){
            FOR(j, d, N){
                if(A[i][j].val() != 0){
                    x = i; y = j;
                    break;
                }
            }
            if(x != -1) break;
        }
        if(x == -1) return false;
        if(x != d){
            swap_row(A, x, d);
            factor *= -1;
        }
        if(y != d){
            swap_column(A, y, d);
            factor *= -1;
        }
        FOR(i, d + 1, N){
            mint a = A[i][d] / A[d][d];
            sbt_row(A, i, d, a);
        }
        FOR(j, 0, d){
            mint a = A[d][j] / A[d][d];
            sbt_column(A, j, d, a);
        }
    }
    IFOR(d, rankB, N){
        FOR(j, 0, d){
            mint a = A[d][j] / A[d][d];
            sbt_column(A, j, d, a);
        }
        factor *= A[d][d];
    }

    mmat X(rankB, mvec(rankB));
    REP(i, rankB) REP(j, rankB) X[i][j] = -A[i][j];
    ans = LibraryChecker::characteristic_polynomial(X);

    ans.resize(N + 1);
    REP(i, N + 1) ans[i] *= factor;

    return true;
}

int main(){

    int N; cin >> N;
    auto A = read_matrix(N);
    auto B = read_matrix(N);

    //auto R = random_matrix(N);
    //auto A0 = mult_matrix(R, A), B0 = mult_matrix(R, B);
    mvec ans(N + 1, 0);
    solve(A, B, ans);

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

    return 0;
}
0