結果

問題 No.1907 DETERMINATION
ユーザー 👑 ygussanyygussany
提出日時 2022-04-15 23:18:44
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 710 ms / 4,000 ms
コード長 3,887 bytes
コンパイル時間 1,001 ms
コンパイル使用メモリ 96,496 KB
実行使用メモリ 5,140 KB
最終ジャッジ日時 2023-08-26 08:46:04
合計ジャッジ時間 25,871 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 219 ms
4,436 KB
testcase_08 AC 94 ms
4,376 KB
testcase_09 AC 155 ms
4,376 KB
testcase_10 AC 508 ms
5,036 KB
testcase_11 AC 255 ms
4,712 KB
testcase_12 AC 490 ms
4,988 KB
testcase_13 AC 463 ms
5,020 KB
testcase_14 AC 430 ms
4,972 KB
testcase_15 AC 91 ms
4,380 KB
testcase_16 AC 37 ms
4,376 KB
testcase_17 AC 479 ms
4,992 KB
testcase_18 AC 332 ms
4,656 KB
testcase_19 AC 13 ms
4,380 KB
testcase_20 AC 471 ms
4,948 KB
testcase_21 AC 52 ms
4,380 KB
testcase_22 AC 455 ms
4,692 KB
testcase_23 AC 546 ms
5,020 KB
testcase_24 AC 163 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 502 ms
5,016 KB
testcase_27 AC 600 ms
4,996 KB
testcase_28 AC 604 ms
4,996 KB
testcase_29 AC 595 ms
5,004 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 515 ms
5,016 KB
testcase_32 AC 500 ms
5,116 KB
testcase_33 AC 503 ms
5,004 KB
testcase_34 AC 502 ms
5,140 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 504 ms
5,080 KB
testcase_39 AC 502 ms
5,016 KB
testcase_40 AC 705 ms
5,004 KB
testcase_41 AC 505 ms
5,128 KB
testcase_42 AC 709 ms
5,004 KB
testcase_43 AC 710 ms
5,020 KB
testcase_44 AC 566 ms
5,116 KB
testcase_45 AC 603 ms
5,108 KB
testcase_46 AC 500 ms
5,064 KB
testcase_47 AC 498 ms
5,088 KB
testcase_48 AC 503 ms
5,128 KB
testcase_49 AC 529 ms
4,996 KB
testcase_50 AC 504 ms
5,092 KB
testcase_51 AC 503 ms
5,100 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 542 ms
4,816 KB
testcase_54 AC 541 ms
4,816 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 543 ms
4,732 KB
testcase_57 AC 542 ms
4,812 KB
testcase_58 AC 415 ms
4,980 KB
testcase_59 AC 352 ms
5,068 KB
testcase_60 AC 355 ms
5,128 KB
testcase_61 AC 430 ms
5,084 KB
testcase_62 AC 359 ms
5,072 KB
testcase_63 AC 503 ms
5,008 KB
testcase_64 AC 2 ms
4,376 KB
testcase_65 AC 1 ms
4,376 KB
testcase_66 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
using namespace std;

#include <atcoder/modint>
using mint = atcoder::static_modint<998244353>;

// Upper Hessenberg reduction of square matrices
// Complexity: O(n^3)
// Reference:
// http://www.phys.uri.edu/nigh/NumRec/bookfpdf/f11-5.pdf
template <class Tp> void hessenberg_reduction(std::vector<std::vector<Tp>> &M) {
    assert(M.size() == M[0].size());
    const int N = M.size();
    for (int r = 0; r < N - 2; r++) {
        int piv = -1;
        for (int j = r + 1; j < N; ++j) if (M[j][r] != 0) {
            piv = j;
            break;
        }
        if (piv < 0) continue;
        for (int i = 0; i < N; i++) std::swap(M[r + 1][i], M[piv][i]);
        for (int i = 0; i < N; i++) std::swap(M[i][r + 1], M[i][piv]);

        const auto rinv = Tp(1) / M[r + 1][r];
        for (int i = r + 2; i < N; i++) {
            const auto n = M[i][r] * rinv;
            for (int j = 0; j < N; j++) M[i][j] -= M[r + 1][j] * n;
            for (int j = 0; j < N; j++) M[j][r + 1] += M[j][i] * n;
        }
    }
}

// Characteristic polynomial of matrix M (|xI - M|)
// Complexity: O(n^3)
// R. Rehman, I. C. Ipsen, "La Budde's Method for Computing Characteristic Polynomials," 2011.
template <class Tp> std::vector<Tp> characteristic_poly(std::vector<std::vector<Tp>> &M) {
    hessenberg_reduction(M);
    const int N = M.size();
    std::vector<std::vector<Tp>> p(N + 1); // p[i + 1] = (Characteristic polynomial of i-th leading principal minor)
    p[0] = {1};
    for (int i = 0; i < N; i++) {
        p[i + 1].assign(i + 2, 0);
        for (int j = 0; j < i + 1; j++) p[i + 1][j + 1] += p[i][j];
        for (int j = 0; j < i + 1; j++) p[i + 1][j] -= p[i][j] * M[i][i];
        Tp betas = 1;
        for (int j = i - 1; j >= 0; j--) {
            betas *= M[j + 1][j];
            Tp hb = -M[j][i] * betas;
            for (int k = 0; k < j + 1; k++) p[i + 1][k] += hb * p[j][k];
        }
    }
    return p[N];
}



int main() {
	int N, a, b = 0;
	mint prod = 1;
	cin >> N;
	vector mat0(N, vector<mint>(N));
    vector mat1(N, vector<mint>(N));
	for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { cin >> a; mat0[i][j] = a; }
	for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { cin >> a; mat1[i][j] = a; }
	for (int i = 0; i < N; ++i) {
        int piv = -1;
        for (int h = i; h < N; ++h) {
            if (mat1[h][i] != 0) piv = h;
        }
        if (piv < 0) {
			for (int h = 0; h < i; h++) {
				for (int hh = 0; hh < N; hh++) mat0[hh][i] -= mat0[hh][h] * mat1[h][i];
				mat1[h][i] = 0;
			}
			b++;
			for (int h = 0; h < N; h++) {
				mat1[h][i] = mat0[h][i];
				mat0[h][i] = 0;
			}
			if (b <= N) {
				i--;
				continue;
			}

			for (int h = 0; h <= N; h++) cout << "0\n";
			return 0;
		}
        assert(piv >= i);
        swap(mat0[i], mat0[piv]);
        swap(mat1[i], mat1[piv]);

        if (i != piv) {
            for (int w = 0; w < N; ++w) {
                mat0[i][w] *= -1;
                mat1[i][w] *= -1;
            }
        }

        mint inv = mat1[i][i].inv();
		prod *= mat1[i][i];
        for (int w = 0; w < N; ++w) {
            mat0[i][w] *= inv;
            mat1[i][w] *= inv;
        }
        for (int h = 0; h < N; ++h) {
            if (h == i) continue;
            if (mat1[h][i] == 0) continue;
            const mint coeff = mat1[h][i];
            for (int w = 0; w < N; ++w) {
                mat1[h][w] -= coeff * mat1[i][w];
                mat0[h][w] -= coeff * mat0[i][w];
            }
        }
    }
	
    for(auto &v : mat0) for (auto &x : v) x = -x;
    auto det_poly = characteristic_poly<mint>(mat0);
	for (int i = b; i <= N; i++) cout << (det_poly[i] * prod).val() << '\n';
	for (int i = N + 1; i <= N + b; i++) cout << "0\n";
	return 0;
}
0