hly1204's library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hly1204/library

:heavy_check_mark: test/matrix/adjugate_matrix.1.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/adjugate_matrix

#include "matrix.hpp"
#include "modint.hpp"
#include <iostream>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    using mint = ModInt<998244353>;
    int n;
    std::cin >> n;
    Matrix<mint> A(n, std::vector<mint>(n));
    std::cin >> A;
    std::cout << A.adj() << '\n';
    return 0;
}
#line 1 "test/matrix/adjugate_matrix.1.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/adjugate_matrix

#line 2 "matrix.hpp"

#include <algorithm>
#include <cassert>
#include <iostream>
#include <optional>
#include <utility>
#include <vector>

template<typename Tp> class Matrix : public std::vector<std::vector<Tp>> {
public:
    using std::vector<std::vector<Tp>>::vector;

    friend std::istream &operator>>(std::istream &a, Matrix &b) {
        const int w = b.width();
        const int h = b.height();
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) a >> b[i][j];
        return a;
    }

    friend std::ostream &operator<<(std::ostream &a, const Matrix &b) {
        const int w = b.width();
        const int h = b.height();
        for (int i = 0; i < h; ++i) {
            if (i) a << '\n';
            for (int j = 0; j < w; ++j) {
                if (j) a << ' ';
                a << b[i][j];
            }
        }
        return a;
    }

    int width() const { return this->empty() ? 0 : (*this)[0].size(); }
    int height() const { return this->size(); }
    bool is_square() const { return width() == height(); }

    static Matrix identity(int n) {
        Matrix A(n, std::vector<Tp>(n));
        for (int i = 0; i < n; ++i) A[i][i] = 1;
        return A;
    }

    Matrix transpose() const {
        const int w = width();
        const int h = height();
        Matrix<Tp> TA(w, std::vector<Tp>(h));
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) TA[j][i] = (*this)[i][j];
        return TA;
    }

    std::vector<Tp> apply(const std::vector<Tp> &b) const {
        const int w = width();
        const int h = height();
        assert((int)b.size() == w);
        std::vector<Tp> Ab(h);
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) Ab[i] += (*this)[i][j] * b[j];
        return Ab;
    }

    Matrix operator-() const {
        Matrix A    = *this;
        const int w = A.width();
        const int h = A.height();
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) A[i][j] = -A[i][j];
        return A;
    }

    Matrix &operator+=(const Matrix<Tp> &B) {
        const int w = width();
        const int h = height();
        assert(w == B.width());
        assert(h == B.height());
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) (*this)[i][j] += B[i][j];
        return *this;
    }

    Matrix &operator-=(const Matrix<Tp> &B) {
        const int w = width();
        const int h = height();
        assert(w == B.width());
        assert(h == B.height());
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) (*this)[i][j] -= B[i][j];
        return *this;
    }

    Matrix operator+(const Matrix<Tp> &B) { return Matrix(*this) += B; }
    Matrix operator-(const Matrix<Tp> &B) { return Matrix(*this) -= B; }
    Matrix operator*(const Tp &b) { return Matrix(*this) *= b; }

    Matrix &operator*=(const Tp &b) {
        const int w = width();
        const int h = height();
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) (*this)[i][j] *= b;
        return *this;
    }

    Matrix operator*(const Matrix<Tp> &B) const {
        const int wA = width();
        const int hA = height();
        assert(B.height() == wA);
        const int wB = B.width();
        Matrix<Tp> AB(hA, std::vector<Tp>(wB));
        for (int i = 0; i < hA; ++i)
            for (int k = 0; k < wA; ++k)
                for (int j = 0; j < wB; ++j) AB[i][j] += (*this)[i][k] * B[k][j];
        return AB;
    }

    Matrix &operator*=(const Matrix<Tp> &B) { return *this = (*this) * B; }

    // returns B s.t. B = X*A in reduced row echelon form.
    Matrix gauss(std::vector<int> *pivot_seq = nullptr, Matrix *X = nullptr) const {
        Matrix B    = *this;
        const int m = B.height();
        const int n = B.width();
        if (X) *X = identity(m);
        for (int i = 0, r = -1 /* r = rank-1 */; i < n; ++i) {
            int pivot = r + 1;
            for (; pivot < m; ++pivot)
                if (B[pivot][i] != 0) break;
            if (pivot == m) continue;
            if (pivot != ++r) {
                B[pivot].swap(B[r]);
                if (X) (*X)[pivot].swap((*X)[r]);
            }
            if (pivot_seq) pivot_seq->push_back(i);
            if (B[r][i] != 1) {
                const auto iv = B[r][i].inv();
                for (int j = i; j < n; ++j) B[r][j] *= iv;
                if (X)
                    for (int j = 0; j < m; ++j) (*X)[r][j] *= iv;
            }
            for (int j = 0; j < m; ++j)
                if (j != r) {
                    const auto p = B[j][i];
                    if (p == 0) continue;
                    for (int k = i; k < n; ++k) B[j][k] -= p * B[r][k];
                    if (X)
                        for (int k = 0; k < m; ++k) (*X)[j][k] -= p * (*X)[r][k];
                }
        }
        return B;
    }

    Tp tr() const {
        assert(is_square());
        Tp s;
        for (int i = 0; i < height(); ++i) s += (*this)[i][i];
        return s;
    }

    Tp det() const {
        assert(is_square());
        Matrix A    = *this;
        const int n = A.height();
        Tp d        = 1;
        for (int i = 0; i < n; ++i) {
            int pivot = i;
            for (; pivot < n; ++pivot)
                if (A[pivot][i] != 0) break;
            if (pivot == n) return 0;
            if (pivot != i) {
                A[pivot].swap(A[i]);
                d = -d;
            }
            d *= A[i][i];
            const auto iv = A[i][i].inv();
            for (int j = i + 1; j < n; ++j) {
                const auto p = A[j][i] * iv;
                if (p == 0) continue;
                for (int k = i; k < n; ++k) A[j][k] -= p * A[i][k];
            }
        }
        return d;
    }

    int rank() const {
        std::vector<int> p;
        std::ignore = gauss(&p);
        return (int)p.size();
    }

    std::optional<Matrix<Tp>> inv() const {
        assert(is_square());
        std::vector<int> p;
        Matrix X;
        std::ignore = gauss(&p, &X);
        if ((int)p.size() < height()) return {};
        return X;
    }

    Matrix to_upper_hessenberg() const {
        assert(is_square());
        Matrix A    = *this;
        const int n = A.height();
        for (int i = 0; i < n - 1; ++i) {
            int pivot = i + 1;
            for (; pivot < n; ++pivot)
                if (A[pivot][i] != 0) break;
            if (pivot == n) continue;
            if (pivot != i + 1) {
                A[pivot].swap(A[i + 1]);
                for (int j = 0; j < n; ++j) std::swap(A[j][pivot], A[j][i + 1]);
            }
            const auto iv = A[i + 1][i].inv();
            for (int j = i + 2; j < n; ++j) {
                if (A[j][i] == 0) continue;
                const auto v = A[j][i] * iv;
                for (int k = i; k < n; ++k) A[j][k] -= v * A[i + 1][k];
                for (int k = 0; k < n; ++k) A[k][i + 1] += v * A[k][j];
            }
        }
        return A;
    }

    // returns det(xI - A)
    std::vector<Tp> charpoly() const {
        const auto H = to_upper_hessenberg();
        const int n  = H.height();
        std::vector<std::vector<Tp>> P(n + 1);
        P[0] = {Tp(1)};
        for (int i = 1; i <= n; ++i) {
            P[i].resize(i + 1);
            for (int j = 0; j < i; ++j)
                P[i][j] -= H[i - 1][i - 1] * P[i - 1][j], P[i][j + 1] += P[i - 1][j];
            Tp t = 1;
            for (int j = 1; j < i; ++j) {
                t *= H[i - j][i - j - 1];
                const auto prod = t * H[i - j - 1][i - 1];
                if (prod == 0) continue;
                for (int k = 0; k < i - j; ++k) P[i][k] -= prod * P[i - j - 1][k];
            }
        }
        return P[n];
    }

    Matrix adj() const {
        // We could reduce the constant factor by using a special Gaussian elimination procedure.
        assert(is_square());
        const int n = height();
        std::vector<int> p;
        Matrix L /* inv or basis of left kernel */, B = gauss(&p, &L);
        // A adj(A) = adj(A) A = det(A) I
        if ((int)p.size() == n) return L * det();
        if ((int)p.size() == n - 1) {
            L.erase(L.begin(), L.begin() + (n - 1));
            Matrix R(n, std::vector<Tp>(1)); // basis of right kernel
            int free = 0;                    // free column (column without pivot)
            while (free < (int)p.size() /* in case A = [0] */ && p[free] == free) ++free;
            // Read from rref
            for (int i = 0; i < n; ++i) R[i][0] = i != free ? -B[i][free] : Tp(1);
            // A adj(A) = adj(A) A = 0 => adj(A) = alpha R L
            Matrix RL = R * L;
            for (int i = 0; i < n; ++i)
                for (int j = 0; j < n; ++j)
                    // find alpha by finding any non-zero entry of RL
                    if (RL[i][j] != 0) {
                        Matrix A = *this;
                        for (int k = 0; k < n; ++k) A[k].erase(A[k].begin() + i);
                        A.erase(A.begin() + j);
                        RL *= A.det() / RL[i][j];
                        if ((i + j) & 1) RL = -RL;
                        break;
                    }
            return RL;
        }
        return Matrix(n, std::vector<Tp>(n));
    }

    // randomized algorithm based on Elegia's trick
    // defined in basis.hpp
    inline std::vector<Tp> minpoly() const;

    // To find A^k(i, j) for large k, the O.G.F. of A^[0, inf)(i, j) is rational,
    // so we could apply Berlekamp--Massey's algorithm to e_i^T * A^[0, n*2) * e_j.
};
#line 2 "modint.hpp"

#line 4 "modint.hpp"
#include <type_traits>

// clang-format off
template<unsigned Mod> class ModInt {
    static_assert((Mod >> 31) == 0, "`Mod` must less than 2^(31)");
    template<typename Int>
    static std::enable_if_t<std::is_integral_v<Int>, unsigned> safe_mod(Int v) { using D = std::common_type_t<Int, unsigned>; return (v %= (int)Mod) < 0 ? (D)(v + (int)Mod) : (D)v; }
    struct PrivateConstructor {} static inline private_constructor{};
    ModInt(PrivateConstructor, unsigned v) : v_(v) {}
    unsigned v_;

public:
    static unsigned mod() { return Mod; }
    static ModInt from_raw(unsigned v) { return ModInt(private_constructor, v); }
    static ModInt zero() { return from_raw(0); }
    static ModInt one() { return from_raw(1); }
    bool is_zero() const { return v_ == 0; }
    bool is_one() const { return v_ == 1; }
    ModInt() : v_() {}
    template<typename Int, typename std::enable_if_t<std::is_signed_v<Int>, int> = 0> ModInt(Int v) : v_(safe_mod(v)) {}
    template<typename Int, typename std::enable_if_t<std::is_unsigned_v<Int>, int> = 0> ModInt(Int v) : v_(v % Mod) {}
    unsigned val() const { return v_; }
    ModInt operator-() const { return from_raw(v_ == 0 ? v_ : Mod - v_); }
    ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); for (ModInt x(*this), res(from_raw(1));; x *= x) { if (e & 1) res *= x; if ((e >>= 1) == 0) return res; }}
    ModInt inv() const { int x1 = 1, x3 = 0, a = val(), b = Mod; while (b) { const int q = a / b, x1_old = x1, a_old = a; x1 = x3, x3 = x1_old - x3 * q, a = b, b = a_old - b * q; } return from_raw(x1 < 0 ? x1 + (int)Mod : x1); }
    template<bool Odd = (Mod & 1)> std::enable_if_t<Odd, ModInt> div_by_2() const { if (v_ & 1) return from_raw((v_ + Mod) >> 1); return from_raw(v_ >> 1); }
    ModInt &operator+=(const ModInt &a) { if ((v_ += a.v_) >= Mod) v_ -= Mod; return *this; }
    ModInt &operator-=(const ModInt &a) { if ((v_ += Mod - a.v_) >= Mod) v_ -= Mod; return *this; }
    ModInt &operator*=(const ModInt &a) { v_ = (unsigned long long)v_ * a.v_ % Mod; return *this; }
    ModInt &operator/=(const ModInt &a) { return *this *= a.inv(); }
    ModInt &operator++() { return *this += one(); }
    ModInt operator++(int) { ModInt o(*this); *this += one(); return o; }
    ModInt &operator--() { return *this -= one(); }
    ModInt operator--(int) { ModInt o(*this); *this -= one(); return o; }
    friend ModInt operator+(const ModInt &a, const ModInt &b) { return ModInt(a) += b; }
    friend ModInt operator-(const ModInt &a, const ModInt &b) { return ModInt(a) -= b; }
    friend ModInt operator*(const ModInt &a, const ModInt &b) { return ModInt(a) *= b; }
    friend ModInt operator/(const ModInt &a, const ModInt &b) { return ModInt(a) /= b; }
    friend bool operator==(const ModInt &a, const ModInt &b) { return a.v_ == b.v_; }
    friend bool operator!=(const ModInt &a, const ModInt &b) { return a.v_ != b.v_; }
    friend std::istream &operator>>(std::istream &a, ModInt &b) { int v; a >> v; b.v_ = safe_mod(v); return a; }
    friend std::ostream &operator<<(std::ostream &a, const ModInt &b) { return a << b.val(); }
};
// clang-format on
#line 6 "test/matrix/adjugate_matrix.1.test.cpp"

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    using mint = ModInt<998244353>;
    int n;
    std::cin >> n;
    Matrix<mint> A(n, std::vector<mint>(n));
    std::cin >> A;
    std::cout << A.adj() << '\n';
    return 0;
}

Test cases

Env Name Status Elapsed Memory
clang++ almost_identity_00 :heavy_check_mark: AC 53 ms 19 MB
clang++ almost_perm_max_random_00 :heavy_check_mark: AC 50 ms 20 MB
clang++ almost_perm_max_random_01 :heavy_check_mark: AC 72 ms 20 MB
clang++ almost_perm_max_random_02 :heavy_check_mark: AC 54 ms 20 MB
clang++ anti55588_00 :heavy_check_mark: AC 16 ms 8 MB
clang++ example_00 :heavy_check_mark: AC 15 ms 8 MB
clang++ example_01 :heavy_check_mark: AC 12 ms 8 MB
clang++ example_02 :heavy_check_mark: AC 15 ms 8 MB
clang++ example_03 :heavy_check_mark: AC 15 ms 8 MB
clang++ highrank_max_random_00 :heavy_check_mark: AC 1993 ms 27 MB
clang++ highrank_max_random_01 :heavy_check_mark: AC 2007 ms 28 MB
clang++ highrank_max_random_02 :heavy_check_mark: AC 1981 ms 27 MB
clang++ highrank_max_random_03 :heavy_check_mark: AC 1992 ms 25 MB
clang++ highrank_max_random_04 :heavy_check_mark: AC 1979 ms 27 MB
clang++ lowrank_max_random_00 :heavy_check_mark: AC 868 ms 16 MB
clang++ lowrank_max_random_01 :heavy_check_mark: AC 716 ms 16 MB
clang++ lowrank_max_random_02 :heavy_check_mark: AC 1089 ms 16 MB
clang++ lowrank_max_random_03 :heavy_check_mark: AC 713 ms 16 MB
clang++ lowrank_max_random_04 :heavy_check_mark: AC 871 ms 16 MB
clang++ max_random_00 :heavy_check_mark: AC 1529 ms 19 MB
clang++ max_random_01 :heavy_check_mark: AC 1531 ms 19 MB
clang++ max_random_02 :heavy_check_mark: AC 1544 ms 19 MB
clang++ max_random_03 :heavy_check_mark: AC 1532 ms 19 MB
clang++ max_random_04 :heavy_check_mark: AC 1554 ms 19 MB
clang++ nontrivial_frobenius_form_00 :heavy_check_mark: AC 50 ms 19 MB
clang++ nontrivial_frobenius_form_01 :heavy_check_mark: AC 1536 ms 19 MB
clang++ nontrivial_frobenius_form_02 :heavy_check_mark: AC 1532 ms 19 MB
clang++ nontrivial_frobenius_form_03 :heavy_check_mark: AC 1528 ms 19 MB
clang++ nontrivial_frobenius_form_04 :heavy_check_mark: AC 1528 ms 19 MB
clang++ nontrivial_frobenius_form_05 :heavy_check_mark: AC 1528 ms 19 MB
clang++ nontrivial_frobenius_form_06 :heavy_check_mark: AC 1535 ms 19 MB
clang++ nontrivial_frobenius_form_07 :heavy_check_mark: AC 1535 ms 19 MB
clang++ nontrivial_frobenius_form_08 :heavy_check_mark: AC 1537 ms 19 MB
clang++ nontrivial_frobenius_form_09 :heavy_check_mark: AC 1540 ms 19 MB
clang++ perm_max_random_00 :heavy_check_mark: AC 51 ms 19 MB
clang++ perm_max_random_01 :heavy_check_mark: AC 43 ms 19 MB
clang++ perm_max_random_02 :heavy_check_mark: AC 65 ms 19 MB
clang++ random_00 :heavy_check_mark: AC 91 ms 11 MB
clang++ random_01 :heavy_check_mark: AC 124 ms 12 MB
clang++ random_02 :heavy_check_mark: AC 22 ms 9 MB
clang++ random_03 :heavy_check_mark: AC 107 ms 12 MB
clang++ random_04 :heavy_check_mark: AC 16 ms 8 MB
clang++ signed_overflow_00 :heavy_check_mark: AC 16 ms 8 MB
clang++ unsigned_overflow_00 :heavy_check_mark: AC 15 ms 8 MB
Back to top page