Asa's CP Library

a01sa01to's competitive programming library.

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

View the Project on GitHub a01sa01to/cp-library

:heavy_check_mark: tests/matrix/determinant/libchecker-arbmod.test.cpp

Depends on

Code

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

#include "../../../cpp/library/data-structure/matrix.hpp"
#include "../../../cpp/library/data-structure/modint.hpp"
#include "../../../cpp/library/matrix/determinant.hpp"
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_det_arbitrary_mod"

using mint = asalib::ds::dynamic_modint<0>;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  mint::set_mod(m);
  asalib::matrix::Matrix<mint> A(n, n);
  rep(i, n) rep(j, n) {
    int a;
    cin >> a;
    A.at(i, j) = a;
  }
  cout << A.determinant().val() << endl;
  return 0;
}
#line 1 "tests/matrix/determinant/libchecker-arbmod.test.cpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

#line 2 "library/data-structure/matrix.hpp"

#line 4 "library/data-structure/matrix.hpp"
using namespace std;

#line 2 "library/_internal/types.hpp"

#include <concepts>
#include <type_traits>
using namespace std;

#line 2 "library/_internal/modint-base.hpp"

#line 5 "library/_internal/modint-base.hpp"
using namespace std;

namespace asalib {
  namespace _internal {
    class modint_base {};

    template<typename T>
    concept is_modint = is_base_of_v<modint_base, T>;
  }  // namespace _internal
}  // namespace asalib
#line 8 "library/_internal/types.hpp"

namespace asalib {
  namespace _internal {
    // ---------- concept definition ---------- //
    template<class T>
    concept integral_like = integral<T> || is_modint<T>;

    template<class T>
    concept floating_like = floating_point<T>;

    template<class T>
    concept numeric_like = integral_like<T> || floating_like<T>;

    // ---------- function definition ---------- //
    template<class T>
    T plus(T a, T b) { return a + b; }

    template<class T>
    T minus(T a, T b) { return a - b; }

    // ---------- constant definition ---------- //
    template<class T>
    T zero() { return 0; }
  }  // namespace _internal
}  // namespace asalib
#line 7 "library/data-structure/matrix.hpp"

namespace asalib {
  namespace matrix {
    template<_internal::numeric_like T>
    class Matrix {
      public:
      constexpr Matrix(): _n_row(0), _n_col(0) {};
      constexpr Matrix(size_t n_row, size_t n_col): _n_row(n_row), _n_col(n_col), _data(n_row * n_col) {};
      constexpr Matrix(size_t n_row, size_t n_col, T x): _n_row(n_row), _n_col(n_col), _data(n_row * n_col, x) {};

      // constexpr T& operator[](size_t i, size_t j) { return _data[i * _n_col + j]; }
      // constexpr const T& operator[](size_t i, size_t j) const { return _data[i * _n_col + j]; }
      // 使えないっぽいので at で代用
      constexpr inline T& at(size_t i, size_t j) { return _data[i * _n_col + j]; }
      constexpr T at(size_t i, size_t j) const { return _data[i * _n_col + j]; }

      constexpr valarray<T> row(size_t i) const { return valarray<T>(_data[slice(i * _n_col, _n_col, 1)]); }
      constexpr valarray<T> col(size_t j) const { return valarray<T>(_data[slice(j, _n_row, _n_col)]); }

      constexpr Matrix operator+=(const T& x) {
        _data += x;
        return *this;
      }
      constexpr Matrix operator-=(const T& x) {
        _data -= x;
        return *this;
      }
      constexpr Matrix operator*=(const T& x) {
        _data *= x;
        return *this;
      }
      constexpr Matrix operator/=(const T& x) {
        _data /= x;
        return *this;
      }
      constexpr Matrix operator%=(const T& x) {
        _data %= x;
        return *this;
      }
      constexpr Matrix operator+(const T& x) const { return Matrix(*this) += x; }
      constexpr Matrix operator-(const T& x) const { return Matrix(*this) -= x; }
      constexpr Matrix operator*(const T& x) const { return Matrix(*this) *= x; }
      constexpr Matrix operator/(const T& x) const { return Matrix(*this) /= x; }
      constexpr Matrix operator%(const T& x) const { return Matrix(*this) %= x; }

      constexpr Matrix operator+=(const Matrix& x) {
        assert(_n_row == x._n_row);
        assert(_n_col == x._n_col);
        _data += x._data;
        return *this;
      }
      constexpr Matrix operator-=(const Matrix& x) {
        assert(_n_row == x._n_row);
        assert(_n_col == x._n_col);
        _data -= x._data;
        return *this;
      }
      constexpr Matrix operator*=(const Matrix& x) {
        assert(_n_col == x._n_row);
        Matrix res(_n_row, x._n_col);
        for (size_t i = 0; i < _n_row; ++i) {
          for (size_t j = 0; j < x._n_col; ++j) {
            res.at(i, j) = (this->_data[slice(i * _n_col, _n_col, 1)] * x._data[slice(j, x._n_row, x._n_col)]).sum();
          }
        }
        return *this = res;
      }
      constexpr Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
      constexpr Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
      constexpr Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }

      constexpr bool operator==(const Matrix& x) const { return _n_row == x._n_row && _n_col == x._n_col && _data == x._data; }
      constexpr bool operator!=(const Matrix& x) const { return !(*this == x); }
      constexpr bool operator<(const Matrix& x) const { return _data < x._data; }

      constexpr const Matrix transpose() const {
        Matrix res(_n_col, _n_row);
        for (size_t i = 0; i < _n_row; ++i) res._data[slice(i, _n_col, _n_row)] = _data[slice(i * _n_col, _n_col, 1)];
        return res;
      }

      template<integral U>
      constexpr Matrix pow(U x) {
        assert(_n_row == _n_col);
        Matrix res = I(_n_row);
        Matrix a(*this);
        while (x) {
          if (x & 1) res *= a;
          a *= a;
          x >>= 1;
        }
        return res;
      }

      constexpr static Matrix I(size_t n) {
        Matrix res(n, n);
        res._data[slice(0, n, n + 1)] = 1;
        return res;
      }

      constexpr size_t n_row() const { return _n_row; }
      constexpr size_t n_col() const { return _n_col; }

      private:
      size_t _n_row, _n_col;
      valarray<T> _data;

      public:
      // ---------- prototype ---------- //
      constexpr T determinant() const;
      template<_internal::numeric_like U>
      constexpr U determinant() const;
    };
  }  // namespace matrix
}  // namespace asalib
#line 2 "library/data-structure/modint.hpp"

#line 6 "library/data-structure/modint.hpp"
using namespace std;

#line 2 "library/math/extgcd.hpp"

#line 4 "library/math/extgcd.hpp"
#include <optional>
#line 6 "library/math/extgcd.hpp"
using namespace std;

namespace asalib {
  namespace math {
    // Returns a pair (x, y) such that ax + by = c
    template<integral T>
    constexpr optional<pair<T, T>> extgcd(T a, T b, T c) {
      if (b == 0) {
        if (c % a != 0) return nullopt;
        return make_pair(c / a, 0);
      }
      auto res = extgcd(b, a % b, c);
      if (!res) return nullopt;
      auto [x, y] = *res;
      return make_pair(y, x - (a / b) * y);
    }
  }  // namespace math
}  // namespace asalib
#line 10 "library/data-structure/modint.hpp"

namespace asalib {
  namespace ds {
    template<unsigned int mod>
      requires(mod >= 1)
    class static_modint: private _internal::modint_base {
      using mint = static_modint;

      public:
      constexpr static_modint(): _val(0) {};

      template<integral T>
      constexpr static_modint(T x) {
        long long y = x % (long long) mod;
        if (y < 0) y += mod;
        _val = y;
      }

      friend constexpr mint operator+(const mint& l, const mint& r) { return mint(l) += r; }
      friend constexpr mint operator-(const mint& l, const mint& r) { return mint(l) -= r; }
      friend constexpr mint operator*(const mint& l, const mint& r) { return mint(l) *= r; }
      friend constexpr mint operator/(const mint& l, const mint& r) { return mint(l) /= r; }
      constexpr mint operator+() const { return *this; }
      constexpr mint operator-() const { return 0 - *this; }

      constexpr mint& operator+=(const mint& other) {
        _val += other._val;
        if (_val >= mod) _val -= mod;
        return *this;
      }
      constexpr mint& operator-=(const mint& other) {
        _val -= other._val;
        if (_val >= mod) _val += mod;
        return *this;
      }
      constexpr mint& operator*=(const mint& other) {
        unsigned long long z = _val;
        z *= other._val;
        _val = z % mod;
        return *this;
      }
      constexpr mint& operator/=(const mint& other) { return *this = *this * other.inv(); }

      constexpr mint& operator++() {
        _val++;
        if (_val == mod) _val = 0;
        return *this;
      }
      constexpr mint& operator--() {
        if (_val == 0) _val = mod;
        _val--;
        return *this;
      }
      constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
      }
      constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
      }

      friend constexpr bool operator==(const mint& l, const mint& r) { return l._val == r._val; }
      friend constexpr bool operator!=(const mint& l, const mint& r) { return !(l == r); }
      friend constexpr bool operator<(const mint& l, const mint& r) { return l._val < r._val; }

      template<integral T>
      constexpr mint pow(T x) const {
        assert(x >= 0);
        mint res = 1, base = *this;
        while (x) {
          if (x & 1) res *= base;
          base *= base;
          x >>= 1;
        }
        return res;
      }

      constexpr mint inv() const {
        if (is_prime_mod()) return pow(mod - 2);
        if (gcd(_val, mod) != 1) throw invalid_argument("Modular inverse does not exist");
        return mint(math::extgcd<long long>(_val, mod, 1).value().first);
      }

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

      private:
      unsigned int _val;
      static constexpr bool is_prime_mod() {
        for (unsigned int i = 2; i * i <= mod; ++i) {
          if (mod % i == 0) return false;
        }
        return true;
      }
    };

    template<unsigned int id>
    class dynamic_modint: private _internal::modint_base {
      using mint = dynamic_modint;

      public:
      constexpr dynamic_modint(): _val(0) {}

      template<integral T>
      constexpr dynamic_modint(T x) {
        assert(_mod >= 1);
        long long y = x % (long long) _mod;
        if (y < 0) y += _mod;
        _val = y;
      };

      friend constexpr mint operator+(const mint& l, const mint& r) { return mint(l) += r; }
      friend constexpr mint operator-(const mint& l, const mint& r) { return mint(l) -= r; }
      friend constexpr mint operator*(const mint& l, const mint& r) { return mint(l) *= r; }
      friend constexpr mint operator/(const mint& l, const mint& r) { return mint(l) /= r; }
      constexpr mint operator+() const { return *this; }
      constexpr mint operator-() const { return 0 - *this; }

      constexpr mint& operator+=(const mint& other) {
        _val += other._val;
        if (_val >= _mod) _val -= _mod;
        return *this;
      }
      constexpr mint& operator-=(const mint& other) {
        _val -= other._val;
        if (_val >= _mod) _val += _mod;
        return *this;
      }
      constexpr mint& operator*=(const mint& other) {
        unsigned long long z = _val;
        z *= other._val;
        _val = z % _mod;
        return *this;
      }
      constexpr mint& operator/=(const mint& other) { return *this = *this * other.inv(); }

      constexpr mint& operator++() {
        _val++;
        if (_val == _mod) _val = 0;
        return *this;
      }
      constexpr mint& operator--() {
        if (_val == 0) _val = _mod;
        _val--;
        return *this;
      }
      constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
      }
      constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
      }

      friend constexpr bool operator==(const mint& l, const mint& r) { return l._val == r._val && l._mod == r._mod; }
      friend constexpr bool operator!=(const mint& l, const mint& r) { return !(l == r); }
      friend constexpr bool operator<(const mint& l, const mint& r) { return l._val < r._val; }

      template<integral T>
      constexpr mint pow(T x) const {
        assert(x >= 0);
        mint res = 1, base = *this;
        while (x) {
          if (x & 1) res *= base;
          base *= base;
          x >>= 1;
        }
        return res;
      }

      constexpr mint inv() const {
        if (gcd(_val, _mod) != 1) throw invalid_argument("Modular inverse does not exist");
        return mint(math::extgcd<long long>(_val, _mod, 1).value().first);
      }

      constexpr unsigned int val() const { return _val; }
      constexpr static unsigned int mod() { return _mod; }
      constexpr static void set_mod(unsigned int mod) {
        assert(mod >= 1);
        _mod = mod;
      }

      private:
      unsigned int _val;
      static inline unsigned int _mod;
    };
  }  // namespace ds
}  // namespace asalib
#line 2 "library/matrix/determinant.hpp"

#line 5 "library/matrix/determinant.hpp"

namespace asalib {
  namespace matrix {
    template<_internal::numeric_like T>
    constexpr T Matrix<T>::determinant() const { return determinant<T>(); }

    template<_internal::numeric_like T>
    template<_internal::numeric_like U>
    constexpr U Matrix<T>::determinant() const {
      assert(_n_row == _n_col);
      valarray<U> t(_data);
      U res = 1;
      for (size_t i = 0; i < _n_row; ++i) {
        if (t[i * _n_col + i] == 0) {
          // ピボットが 0 のとき、他の行と交換
          for (size_t j = i + 1; j < _n_row; ++j) {
            if (t[j * _n_col + i] != 0) {
              for (size_t k = 0; k < _n_col; ++k) swap(t[j * _n_col + k], t[i * _n_col + k]);
              res *= -1;
              break;
            }
          }
        }
        if (t[i * _n_col + i] == 0) return 0;
        // 掃き出し
        for (size_t j = i + 1; j < _n_row; ++j) {
          if (t[j * _n_col + i] == 0) continue;
          try {
            U r = t[j * _n_col + i] / t[i * _n_col + i];
            t[slice(j * _n_col + i, _n_col - i, 1)] -= r * valarray<U>(t[slice(i * _n_col + i, _n_col - i, 1)]);
          }
          catch (...) {
            // t[i][i] != 0 なのでゼロ割りは発生しない -> modint で逆元がないときにこっちに来る
            assert(_internal::is_modint<U>);
            // 以下の方法は modint 以外でも使える (はず) だが、 q 計算で .val してるので try-catch にしてやる
            // 拡張ユークリッドして、 A * (t[i][i], t[j][i]) = (gcd, 0) となるような 2x2 の A を作ってやる
            // | a  b | | tii | = | x |
            // | c  d | | tji |   | y |
            // となるように保持する
            // 初期値は a = 1, b = 0, x = tii; c = 0, d = 1, y = tji;
            U a = 1, b = 0, c = 0, d = 1, x = t[i * _n_col + i], y = t[j * _n_col + i];
            bool neg = false;
            while (y != 0) {
              // ただの数だと思って計算するといいらしい?
              U q = x.val() / y.val();
              U z = x - q * y;
              U e = a - q * c, f = b - q * d;
              x = y, y = z, a = c, b = d, c = e, d = f;
              neg = !neg;
            }
            if (neg) c = -c, d = -d;
            assert(a * t[i * _n_col + i] + b * t[j * _n_col + i] == x);
            assert(c * t[i * _n_col + i] + d * t[j * _n_col + i] == 0);
            valarray<U> tmpi = t[slice(i * _n_col + i, _n_col - i, 1)];
            valarray<U> tmpj = t[slice(j * _n_col + i, _n_col - i, 1)];
            t[slice(i * _n_col + i, _n_col - i, 1)] = a * tmpi + b * tmpj;  // なんでこっちも更新するんだろ、よくわからん
            t[slice(j * _n_col + i, _n_col - i, 1)] = c * tmpi + d * tmpj;
            assert(t[i * _n_col + i] == x);
            assert(t[j * _n_col + i] == 0);
          }
        }
      }
      for (size_t i = 0; i < _n_row; ++i) res *= t[i * _n_col + i];
      return res;
    }
  }  // namespace matrix
}  // namespace asalib
#line 10 "tests/matrix/determinant/libchecker-arbmod.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_det_arbitrary_mod"

using mint = asalib::ds::dynamic_modint<0>;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  mint::set_mod(m);
  asalib::matrix::Matrix<mint> A(n, n);
  rep(i, n) rep(j, n) {
    int a;
    cin >> a;
    A.at(i, j) = a;
  }
  cout << A.determinant().val() << endl;
  return 0;
}
Back to top page