結果

問題 No.1907 DETERMINATION
ユーザー 👑 colognecologne
提出日時 2022-05-20 14:58:40
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,725 bytes
コンパイル時間 2,204 ms
コンパイル使用メモリ 117,156 KB
実行使用メモリ 7,192 KB
最終ジャッジ日時 2023-10-20 03:09:59
合計ジャッジ時間 31,363 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 82 ms
4,752 KB
testcase_09 AC 139 ms
5,232 KB
testcase_10 AC 489 ms
7,080 KB
testcase_11 AC 194 ms
5,624 KB
testcase_12 WA -
testcase_13 AC 452 ms
7,004 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 29 ms
4,348 KB
testcase_17 AC 456 ms
7,020 KB
testcase_18 WA -
testcase_19 AC 9 ms
4,348 KB
testcase_20 WA -
testcase_21 AC 42 ms
4,368 KB
testcase_22 AC 419 ms
5,624 KB
testcase_23 AC 530 ms
7,064 KB
testcase_24 AC 147 ms
5,232 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 490 ms
7,192 KB
testcase_27 AC 591 ms
7,192 KB
testcase_28 AC 595 ms
7,192 KB
testcase_29 AC 593 ms
7,192 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 502 ms
7,192 KB
testcase_32 WA -
testcase_33 AC 491 ms
7,192 KB
testcase_34 AC 497 ms
7,192 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 490 ms
7,192 KB
testcase_39 AC 494 ms
7,192 KB
testcase_40 AC 712 ms
7,192 KB
testcase_41 AC 487 ms
7,192 KB
testcase_42 AC 707 ms
7,192 KB
testcase_43 AC 698 ms
7,192 KB
testcase_44 AC 553 ms
7,192 KB
testcase_45 AC 591 ms
7,192 KB
testcase_46 AC 480 ms
7,144 KB
testcase_47 WA -
testcase_48 AC 485 ms
7,192 KB
testcase_49 AC 523 ms
7,192 KB
testcase_50 AC 489 ms
7,192 KB
testcase_51 AC 491 ms
7,192 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 510 ms
5,888 KB
testcase_54 AC 506 ms
5,888 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 503 ms
5,888 KB
testcase_57 AC 509 ms
5,888 KB
testcase_58 AC 408 ms
7,144 KB
testcase_59 WA -
testcase_60 AC 314 ms
7,192 KB
testcase_61 AC 402 ms
7,192 KB
testcase_62 AC 317 ms
7,192 KB
testcase_63 AC 494 ms
7,192 KB
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <vector>

// calculates det(xI-M)
template <typename T>
std::vector<T> char_poly(std::vector<std::vector<T>> M)
{
    int N = M.size();

    // Hessenberg Reduction
    for (int i = 0; i < N - 2; i++)
    {
        int p = -1;
        for (int j = i + 1; j < N; j++)
            if (M[j][i] != T(0))
            {
                p = j;
                break;
            }
        if (p == -1)
            continue;
        std::swap(M[i + 1], M[p]);
        for (int j = 0; j < N; j++)
            std::swap(M[j][i + 1], M[j][p]);

        T r = T(1) / M[i + 1][i];
        for (int j = i + 2; j < N; j++)
        {
            T c = M[j][i] * r;
            for (int k = 0; k < N; k++)
                M[j][k] -= M[i + 1][k] * c;
            for (int k = 0; k < N; k++)
                M[k][i + 1] += M[k][j] * c;
        }
    }

    // La Budde's Method
    std::vector<std::vector<T>> P = {{T(1)}};
    for (int i = 0; i < N; i++)
    {
        std::vector<T> f(i + 2, 0);
        for (int j = 0; j <= i; j++)
            f[j + 1] += P[i][j];
        for (int j = 0; j <= i; j++)
            f[j] -= P[i][j] * M[i][i];

        T b = 1;
        for (int j = i - 1; j >= 0; j--)
        {
            b *= M[j + 1][j];
            T h = -M[j][i] * b;
            for (int k = 0; k <= j; k++)
                f[k] += h * P[j][k];
        }
        P.push_back(f);
    }
    return P.back();
}

// Calculates det(Ax+B)
template <typename T>
std::vector<T> det_linear(std::vector<std::vector<T>> A, std::vector<std::vector<T>> B)
{
    int N = A.size(), nu = 0;
    T det = 1;

    for (int i = 0; i < N; i++)
    {
        // do normal gaussian elimination
        int p = -1;
        for (int j = i; j < N; j++)
            if (A[j][i] != T(0))
            {
                p = j;
                break;
            }

        // replace B with A
        if (p == -1)
        {
            // Increase nullity by 1
            if (++nu > N)
                return std::vector<T>(N + 1, 0);

            // i-th column is empty
            for (int r = 0; r < i; r++)
            {
                for (int j = 0; j < N; j++)
                    B[j][i] -= A[r][i] * B[j][r];
                A[r][i] = 0;
            }
            for (int j = 0; j < N; j++)
                std::swap(A[j][i], B[j][i]);

            // retry
            --i;
            continue;
        }

        if (p != i)
        {
            std::swap(A[i], A[p]);
            std::swap(B[i], B[p]);
            det = -det;
        }

        det *= A[i][i];
        T c = T(1) / A[i][i];
        for (int j = 0; j < N; j++)
            A[i][j] *= c, B[i][j] *= c;

        for (int j = 0; j < N; j++)
            if (j != i)
            {
                T c = A[j][i];
                for (int k = 0; k < N; k++)
                    A[j][k] -= A[i][k] * c, B[j][k] -= B[i][k] * c;
            }
    }

    for (auto &y : B)
        for (T &x : y)
            x = -x;

    auto f = char_poly(B);
    for (T &x : f)
        x *= det;

    f.erase(f.begin(), f.begin() + nu);
    f.resize(N + 1);
    return f;
}

#include <iostream>
#include <atcoder/modint>

using namespace std;
using Mint = atcoder::modint998244353;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N;
    cin >> N;
    vector<vector<Mint>> A(N, vector<Mint>(N)), B = A;
    for (auto &y : B)
        for (auto &x : y)
        {
            int v;
            cin >> v;
            x = -v;
        }
    for (auto &y : A)
        for (auto &x : y)
        {
            int v;
            cin >> v;
            x = -v;
        }

    auto poly = det_linear(A, B);
    for (auto v : poly)
        cout << v.val() << endl;
}
0