結果

問題 No.1907 DETERMINATION
ユーザー SumitacchanSumitacchan
提出日時 2022-02-05 19:47:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,036 bytes
コンパイル時間 2,878 ms
コンパイル使用メモリ 222,120 KB
実行使用メモリ 9,584 KB
最終ジャッジ日時 2024-06-06 19:04:02
合計ジャッジ時間 47,298 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 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 2 ms
5,376 KB
testcase_07 AC 409 ms
6,912 KB
testcase_08 AC 164 ms
5,376 KB
testcase_09 AC 278 ms
6,144 KB
testcase_10 AC 948 ms
7,860 KB
testcase_11 AC 583 ms
7,464 KB
testcase_12 AC 1,019 ms
9,164 KB
testcase_13 AC 953 ms
8,828 KB
testcase_14 AC 979 ms
8,912 KB
testcase_15 AC 209 ms
5,760 KB
testcase_16 AC 54 ms
5,376 KB
testcase_17 AC 906 ms
8,072 KB
testcase_18 AC 606 ms
7,104 KB
testcase_19 AC 17 ms
5,376 KB
testcase_20 AC 961 ms
8,744 KB
testcase_21 AC 82 ms
5,376 KB
testcase_22 AC 803 ms
7,596 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 1,046 ms
9,584 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 1,055 ms
9,456 KB
testcase_34 AC 1,061 ms
9,456 KB
testcase_35 WA -
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1,042 ms
9,528 KB
testcase_39 AC 1,053 ms
9,456 KB
testcase_40 AC 939 ms
7,664 KB
testcase_41 AC 1,050 ms
9,456 KB
testcase_42 AC 922 ms
7,540 KB
testcase_43 AC 921 ms
7,664 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 1,072 ms
9,428 KB
testcase_47 AC 1,204 ms
9,452 KB
testcase_48 AC 1,054 ms
9,580 KB
testcase_49 AC 1,058 ms
8,820 KB
testcase_50 AC 1,042 ms
9,456 KB
testcase_51 AC 1,045 ms
9,560 KB
testcase_52 AC 2 ms
5,376 KB
testcase_53 AC 952 ms
7,664 KB
testcase_54 AC 940 ms
7,408 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 933 ms
7,660 KB
testcase_57 AC 936 ms
7,660 KB
testcase_58 AC 794 ms
7,512 KB
testcase_59 AC 910 ms
9,456 KB
testcase_60 AC 913 ms
9,460 KB
testcase_61 AC 962 ms
9,072 KB
testcase_62 AC 914 ms
9,324 KB
testcase_63 AC 1,039 ms
9,456 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);

    mvec ans(N + 1, 0);

    // とりあえずランダム行列かけとく
    auto R = random_matrix(N);
    auto A0 = mult_matrix(R, A), B0 = mult_matrix(R, B);
    if(!solve(A0, B0, ans)){
        // Dが正則にならなかったらだめだけど、
        // だめなら行列式は定数と信じる
        ans[0] = determinant(A0);
    }
    REP(i, N + 1) cout << ans[i].val() << endl;

    return 0;
}
0