Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ebi-fly13/Library

:heavy_check_mark: test/matrix/Determinant_of_Matrix.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/matrix_det"

#include <iostream>


#include "../../matrix/square_matrix.hpp"

#include "../../modint/modint.hpp"


using Matrix = ebi::square_matrix<ebi::modint998244353, 0>;

int main() {
    int n;
    std::cin >> n;
    Matrix::set_size(n);
    Matrix a;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int val;
            std::cin >> val;
            a[i][j] = val;
        }
    }
    std::cout << a.det().val() << std::endl;
}
#line 1 "test/matrix/Determinant_of_Matrix.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_det"

#include <iostream>


#line 2 "matrix/square_matrix.hpp"

#line 4 "matrix/square_matrix.hpp"
#include <vector>


namespace ebi {

template <class Field, int id> struct square_matrix {
  private:
    using Self = square_matrix<Field, id>;

  public:
    square_matrix(Field val = 0)
        : mat(std::vector(N, std::vector<Field>(N, val))) {}

    Self operator+(Self &rhs) const noexcept {
        return Self(*this) += rhs;
    }

    Self operator-(Self &rhs) const noexcept {
        return Self(*this) -= rhs;
    }

    Self operator*(Self &rhs) const noexcept {
        return Self(*this) *= rhs;
    }

    Self &operator+=(Self &rhs) noexcept {
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                mat[i][j] += rhs[i][j];
            }
        }
        return *this;
    }

    Self &operator-=(Self &rhs) noexcept {
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < N; ++j) {
                mat[i][j] -= rhs[i][j];
            }
        }
        return *this;
    }

    Self &operator*=(Self &rhs) noexcept {
        Self ret;
        for (int i = 0; i < N; ++i) {
            for (int k = 0; k < N; ++k) {
                for (int j = 0; j < N; ++j) {
                    ret[i][j] += mat[i][k] * rhs[k][j];
                }
            }
        }
        return *this = ret;
    }

    Self pow(long long n) const {
        Self res;
        for (int i = 0; i < N; ++i) {
            res[i][i] = 1;
        }
        Self x = *this;
        while (n > 0) {
            if (n & 1) res *= x;
            x *= x;
            n >>= 1;
        }
        return res;
    }

    Field det() const {
        Self res = *this;
        Field d = 1;
        for (int i = 0; i < N; ++i) {
            if (res[i][i] == 0) {
                int flag = -1;
                for (int j = i + 1; j < N; ++j) {
                    if (res[j][i] != 0) {
                        flag = j;
                        break;
                    }
                }
                if (flag < 0) {
                    return 0;
                }
                std::swap(res[i], res[flag]);
                d = -d;
            }
            Field inv = res[i][i].inv();
            for (int j = i + 1; j < N; ++j) {
                Field fac = res[j][i] * inv;
                for (int k = i; k < N; ++k) {
                    res[j][k] -= fac * res[i][k];
                }
            }
            d *= res[i][i];
        }
        return d;
    }

    static Self identity() {
        Self res;
        for (int i = 0; i < N; ++i) {
            res[i][i] = 1;
        }
        return res;
    }

    std::vector<Field> &operator[](int i) {
        return mat[i];
    }

    const std::vector<Field> &operator[](int i) const {
        return mat[i];
    }

    static void set_size(int n) {
        N = n;
    }

    int size() const {
        return N;
    }

  private:
    std::vector<std::vector<Field>> mat;
    static int N;
};

template <class Field, int id> int square_matrix<Field, id>::N = 0;

template <class Field, int id>
std::istream &operator>>(std::istream &os, square_matrix<Field, id> &a) {
    int n = a.size();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            os >> a[i][j];
        }
    }
    return os;
}

template <class Field, int id>
std::ostream &operator<<(std::ostream &os, const square_matrix<Field, id> &a) {
    int n = a.size();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            os << a[i][j];
            if (j < n - 1) os << ' ';
        }
        if (i < n - 1) os << '\n';
    }
    return os;
}

}  // namespace ebi
#line 2 "modint/modint.hpp"

#include <cassert>

#line 5 "modint/modint.hpp"

#line 2 "modint/base.hpp"

#include <concepts>
#line 5 "modint/base.hpp"
#include <utility>

namespace ebi {

template <class T>
concept Modint = requires(T a, T b) {
    a + b;
    a - b;
    a * b;
    a / b;
    a.inv();
    a.val();
    a.pow(std::declval<long long>());
    T::mod();
};

template <Modint mint> std::istream &operator>>(std::istream &os, mint &a) {
    long long x;
    os >> x;
    a = x;
    return os;
}

template <Modint mint>
std::ostream &operator<<(std::ostream &os, const mint &a) {
    return os << a.val();
}

}  // namespace ebi
#line 7 "modint/modint.hpp"

namespace ebi {

template <int m> struct static_modint {
  private:
    using modint = static_modint;

  public:
    static constexpr int mod() {
        return m;
    }

    static constexpr modint raw(int v) {
        modint x;
        x._v = v;
        return x;
    }

    constexpr static_modint() : _v(0) {}

    constexpr static_modint(long long v) {
        v %= (long long)umod();
        if (v < 0) v += (long long)umod();
        _v = (unsigned int)v;
    }

    constexpr unsigned int val() const {
        return _v;
    }

    constexpr unsigned int value() const {
        return val();
    }

    constexpr modint &operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    constexpr modint &operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }

    constexpr modint operator++(int) {
        modint res = *this;
        ++*this;
        return res;
    }
    constexpr modint operator--(int) {
        modint res = *this;
        --*this;
        return res;
    }

    constexpr modint &operator+=(const modint &rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    constexpr modint &operator-=(const modint &rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    constexpr modint &operator*=(const modint &rhs) {
        unsigned long long x = _v;
        x *= rhs._v;
        _v = (unsigned int)(x % (unsigned long long)umod());
        return *this;
    }
    constexpr modint &operator/=(const modint &rhs) {
        return *this = *this * rhs.inv();
    }

    constexpr modint operator+() const {
        return *this;
    }
    constexpr modint operator-() const {
        return modint() - *this;
    }

    constexpr modint pow(long long n) const {
        assert(0 <= n);
        modint x = *this, res = 1;
        while (n) {
            if (n & 1) res *= x;
            x *= x;
            n >>= 1;
        }
        return res;
    }
    constexpr modint inv() const {
        assert(_v);
        return pow(umod() - 2);
    }

    friend modint operator+(const modint &lhs, const modint &rhs) {
        return modint(lhs) += rhs;
    }
    friend modint operator-(const modint &lhs, const modint &rhs) {
        return modint(lhs) -= rhs;
    }
    friend modint operator*(const modint &lhs, const modint &rhs) {
        return modint(lhs) *= rhs;
    }

    friend modint operator/(const modint &lhs, const modint &rhs) {
        return modint(lhs) /= rhs;
    }
    friend bool operator==(const modint &lhs, const modint &rhs) {
        return lhs.val() == rhs.val();
    }
    friend bool operator!=(const modint &lhs, const modint &rhs) {
        return !(lhs == rhs);
    }

  private:
    unsigned int _v = 0;

    static constexpr unsigned int umod() {
        return m;
    }
};

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;

}  // namespace ebi
#line 7 "test/matrix/Determinant_of_Matrix.test.cpp"

using Matrix = ebi::square_matrix<ebi::modint998244353, 0>;

int main() {
    int n;
    std::cin >> n;
    Matrix::set_size(n);
    Matrix a;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int val;
            std::cin >> val;
            a[i][j] = val;
        }
    }
    std::cout << a.det().val() << std::endl;
}
Back to top page