結果

問題 No.1907 DETERMINATION
ユーザー SumitacchanSumitacchan
提出日時 2022-02-05 19:47:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,036 bytes
コンパイル時間 3,525 ms
コンパイル使用メモリ 218,716 KB
実行使用メモリ 9,700 KB
最終ジャッジ日時 2023-08-26 00:09:12
合計ジャッジ時間 49,758 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 427 ms
6,796 KB
testcase_08 AC 166 ms
5,200 KB
testcase_09 AC 288 ms
5,992 KB
testcase_10 AC 982 ms
8,036 KB
testcase_11 AC 603 ms
7,296 KB
testcase_12 AC 1,059 ms
9,152 KB
testcase_13 AC 990 ms
8,956 KB
testcase_14 AC 1,002 ms
9,208 KB
testcase_15 AC 218 ms
5,784 KB
testcase_16 AC 58 ms
4,384 KB
testcase_17 AC 942 ms
7,920 KB
testcase_18 AC 632 ms
7,200 KB
testcase_19 AC 17 ms
4,380 KB
testcase_20 AC 1,005 ms
8,716 KB
testcase_21 AC 85 ms
4,488 KB
testcase_22 AC 821 ms
7,216 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 1,101 ms
9,404 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 2 ms
4,380 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1,104 ms
9,428 KB
testcase_34 AC 1,100 ms
9,420 KB
testcase_35 WA -
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1,100 ms
9,652 KB
testcase_39 AC 1,103 ms
9,536 KB
testcase_40 AC 976 ms
7,668 KB
testcase_41 AC 1,092 ms
9,480 KB
testcase_42 AC 975 ms
7,676 KB
testcase_43 AC 975 ms
7,552 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 1,075 ms
9,268 KB
testcase_47 AC 1,093 ms
9,480 KB
testcase_48 AC 1,100 ms
9,460 KB
testcase_49 AC 1,090 ms
9,060 KB
testcase_50 AC 1,099 ms
9,456 KB
testcase_51 AC 1,098 ms
9,700 KB
testcase_52 AC 1 ms
4,380 KB
testcase_53 AC 976 ms
7,544 KB
testcase_54 AC 976 ms
7,552 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 973 ms
7,548 KB
testcase_57 AC 981 ms
7,536 KB
testcase_58 AC 834 ms
7,564 KB
testcase_59 AC 937 ms
9,552 KB
testcase_60 AC 944 ms
9,416 KB
testcase_61 AC 996 ms
8,984 KB
testcase_62 AC 946 ms
9,492 KB
testcase_63 AC 1,092 ms
9,472 KB
testcase_64 AC 2 ms
4,380 KB
testcase_65 AC 2 ms
4,380 KB
testcase_66 AC 2 ms
4,384 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