Asa's CP Library

a01sa01to's competitive programming library. Requires C++20 or higher with GCC. This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub a01sa01to/cp-library

:heavy_check_mark: tests/matrix/core/libchecker-pow.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 "../../../library/data-structure/matrix.hpp"
#include "../../../library/data-structure/modint.hpp"
#include "../../../library/fastio.hpp"
#define PROBLEM "https://judge.yosupo.jp/problem/pow_of_matrix"

using mint = asalib::ds::static_modint<998244353>;

int main() {
  int n;
  ll k;
  asalib::FastIO >> n >> k;
  asalib::matrix::Matrix<mint> A(n, n);
  rep(i, n) rep(j, n) {
    int x;
    asalib::FastIO >> x;
    A.at(i, j) = x;
  }
  auto B = A.pow(k);
  rep(i, n) rep(j, n) asalib::FastIO << B.at(i, j).val() << " \n"[j == n - 1];
  return 0;
}
#line 1 "tests/matrix/core/libchecker-pow.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"
#include <concepts>
#line 7 "library/data-structure/matrix.hpp"
using namespace std;

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

#line 4 "library/_internal/types.hpp"
using namespace std;

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

#line 4 "library/_internal/modint-base.hpp"
#include <type_traits>
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 7 "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 10 "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.resize(n_row, valarray<T>(n_col));
      };
      constexpr Matrix(size_t n_row, size_t n_col, T x): _n_row(n_row), _n_col(n_col) {
        _data.resize(n_row, valarray<T>(n_col, x));
      };

      inline constexpr T& at(size_t i, size_t j) {
        assert(i < _n_row);
        assert(j < _n_col);
        return _data[i][j];
      }
      inline constexpr T at(size_t i, size_t j) const {
        assert(i < _n_row);
        assert(j < _n_col);
        return _data[i][j];
      }

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

      inline constexpr Matrix& operator+=(const Matrix& x) {
        assert(_n_row == x._n_row);
        assert(_n_col == x._n_col);
        _data += x._data;
        return *this;
      }
      inline constexpr Matrix& operator-=(const Matrix& x) {
        assert(_n_row == x._n_row);
        assert(_n_col == x._n_col);
        _data -= x._data;
        return *this;
      }
      inline 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 k = 0; k < _n_col; ++k) {
            for (size_t j = 0; j < x._n_col; ++j) {
              res._data[i][j] += _data[i][k] * x._data[k][j];
            }
          }
        }
        return *this = res;
      }
      inline constexpr Matrix operator+(const Matrix& x) const { return Matrix(*this) += x; }
      inline constexpr Matrix operator-(const Matrix& x) const { return Matrix(*this) -= x; }
      inline constexpr Matrix operator*(const Matrix& x) const { return Matrix(*this) *= x; }

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

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

      template<integral U>
      inline constexpr Matrix pow(U x) const {
        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;
      }

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

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

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

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

#line 8 "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 12 "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;
      using uint = unsigned int;
      using ll = long long;
      using ull = unsigned long long;

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

      template<integral T>
      inline constexpr static_modint(const T& x) {
        if constexpr (is_signed_v<T>) {
          ll y = x % (ll) mod;
          if (y < 0) y += mod;
          _val = y;
        }
        else {
          _val = x % mod;
        }
      }

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

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

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

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

      template<integral T>
      inline 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;
      }

      inline constexpr mint inv() const {
        if constexpr (is_prime_mod) return pow(mod - 2);
        else {
          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 inline unsigned int val() const { return _val; }

      private:
      uint _val;
      static inline 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;
      using uint = unsigned int;
      using ll = long long;
      using ull = unsigned long long;

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

      template<integral T>
      inline constexpr dynamic_modint(const T& x) {
        assert(_mod >= 1);
        if constexpr (is_signed_v<T>) {
          ll y = x % (ll) _mod;
          if (y < 0) y += _mod;
          _val = y;
        }
        else {
          _val = x % _mod;
        }
      };

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

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

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

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

      template<integral T>
      inline 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;
      }

      inline 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);
      }

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

      private:
      uint _val;
      static inline uint _mod;
    };
  }  // namespace ds
}  // namespace asalib
#line 2 "library/fastio.hpp"

#line 12 "library/fastio.hpp"
using namespace std;

// TODO: 何も入力がない場合にも対応する?

namespace asalib {
  namespace _internal {
    class FastIO {
      private:
      using uint = unsigned int;
      static constexpr uint BUFFER_SIZE = 1 << 20;
      static constexpr uint MAX_TOKEN_SIZE = 64;

      // ===== Read ===== //

      private:
      array<char, BUFFER_SIZE> read_buffer;
      array<char, BUFFER_SIZE>::iterator read_ptr;
      uint read_size = 0;

      inline void load() {
        // まだ読んでないデータを前に持ってくる
        memcpy(read_buffer.begin(), read_ptr, read_size - (read_ptr - read_buffer.begin()));
        read_size -= read_ptr - read_buffer.begin();
        read_ptr = read_buffer.begin();
        // stdin から読み込み
        read_size += fread(read_buffer.begin() + read_size, 1, BUFFER_SIZE - read_size, stdin);
      }

      inline void skip_space() {
        // 制御文字 + space
        // DEL (127 = 0x7F) がコーナーケースだがまあ使わんやろ
        while (*read_ptr <= ' ') ++read_ptr;
      }

      template<typename T>
        requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
      inline void _read_uint(T& x) {
        x = 0;
        while (true) {
          uint64_t v;
          memcpy(&v, read_ptr, 8);
          // '0' -> 0 処理
          // こっちを先にやることで制御文字系もはじく
          v -= 0x30'30'30'30'30'30'30'30;
          // ASCII 範囲外
          if (v & 0x80'80'80'80'80'80'80'80) break;
          // 桁ごとの数字から数値に
          v = (v * 10 + (v >> 8)) & 0x00ff00ff00ff00ff;
          v = (v * 100 + (v >> 16)) & 0x0000ffff0000ffff;
          v = (v * 10000 + (v >> 32)) & 0x00000000ffffffff;
          x = 1'0000'0000 * x + v;
          read_ptr += 8;
        }
        while (true) {
          uint32_t v;
          memcpy(&v, read_ptr, 4);
          v -= 0x30'30'30'30;
          if (v & 0x80'80'80'80) break;
          v = (v * 10 + (v >> 8)) & 0x00ff00ff;
          v = (v * 100 + (v >> 16)) & 0x0000ffff;
          x = 1'0000 * x + v;
          read_ptr += 4;
        }
        while (true) {
          uint16_t v;
          memcpy(&v, read_ptr, 2);
          v -= 0x3030;
          if (v & 0x8080) break;
          v = (v * 10 + (v >> 8)) & 0x00ff;
          x = 100 * x + v;
          read_ptr += 2;
        }
        if (*read_ptr > ' ') x = 10 * x + (*read_ptr++ & 0x0f);
      }

      inline void read_commonop() {
        // そろそろ限界なら読み込み
        if ((read_ptr - read_buffer.begin()) + MAX_TOKEN_SIZE >= BUFFER_SIZE) load();
        skip_space();
      }

      public:
      FastIO& operator>>(char& x) {
        read_commonop();
        x = *read_ptr++;
        return *this;
      }

      FastIO& operator>>(string& x) {
        read_commonop();
        x.clear();
        while (*read_ptr > ' ') {
          x += *read_ptr++;
          if (read_ptr == read_buffer.end()) load();
        }
        return *this;
      }

      template<typename T>
        requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
      FastIO& operator>>(T& x) {
        read_commonop();
        constexpr bool is_signed = is_signed_v<T> || is_same_v<T, __int128_t>;
        if constexpr (is_signed) {
          if (*read_ptr == '-') {
            ++read_ptr;
            _read_uint(x);
            x = -x;
            return *this;
          }
        }
        _read_uint(x);
        return *this;
      }

      // ===== Write ===== //

      private:
      array<char, BUFFER_SIZE> write_buffer;
      array<char, BUFFER_SIZE>::iterator write_ptr;
      static constexpr uint MAX_PRECOMPUTE_NUM = 1'0000;
      static constexpr uint MAX_PRECOMPUTE_NUM_DIGIT = 4;

      static constexpr array<char, MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT> digits = [] {
        array<char, MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT> digits {};
        for (uint i = 0; i < MAX_PRECOMPUTE_NUM; ++i) {
          uint x = i;
          for (int j = MAX_PRECOMPUTE_NUM_DIGIT - 1; j >= 0; --j) {
            digits[i * MAX_PRECOMPUTE_NUM_DIGIT + j] = '0' + (x % 10);
            x /= 10;
          }
        }
        return digits;
      }();

      template<typename T>
        requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
      static constexpr array<T, numeric_limits<T>::digits10 + 1> Pow10 = [] {
        array<T, numeric_limits<T>::digits10 + 1> pow10 {};
        pow10[0] = 1;
        for (uint i = 1; i < pow10.size(); ++i) pow10[i] = pow10[i - 1] * 10;
        return pow10;
      }();

      inline void write_commonop() {
        // そろそろ限界なら書き込む
        if ((write_ptr - write_buffer.begin()) + MAX_TOKEN_SIZE >= BUFFER_SIZE) flush();
      }

      inline void putchar(const char& x) { *write_ptr++ = x; }

      template<typename T, int NumDig>
        requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
      inline void _write_uint_top(const T& x) {
        // leading-zero を書き込まないようにする
        if constexpr (NumDig > 1) {
          if (x < Pow10<T>[NumDig - 1]) {
            _write_uint_top<T, NumDig - 1>(x);
            return;
          }
        }
        copy_n(digits.begin() + (x + 1) * MAX_PRECOMPUTE_NUM_DIGIT - NumDig, NumDig, write_ptr);
        write_ptr += NumDig;
      }

      template<typename T, int NumDig>
        requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
      inline void _write_uint(const T& x) {
        if constexpr (NumDig >= 0) {
          if constexpr (NumDig > MAX_PRECOMPUTE_NUM_DIGIT) _write_uint<T, NumDig - MAX_PRECOMPUTE_NUM_DIGIT>(x / MAX_PRECOMPUTE_NUM);
          copy_n(digits.begin() + x % MAX_PRECOMPUTE_NUM * MAX_PRECOMPUTE_NUM_DIGIT, MAX_PRECOMPUTE_NUM_DIGIT, write_ptr);
          write_ptr += MAX_PRECOMPUTE_NUM_DIGIT;
        }
      }

      template<typename T, int NumDig>
        requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
      inline void _write_uint_root(const T& x) {
        if constexpr (is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>) {
          // 128bit 除算はおそいので 64bit 除算を使う
          if (x < Pow10<T>[16]) {
            _write_uint_root<uint64_t, NumDig>(x);
          }
          else if (x < Pow10<T>[32]) {
            _write_uint_root<uint64_t, NumDig>(x / Pow10<T>[16]);
            _write_uint<uint64_t, 16>(x % Pow10<T>[16]);
          }
          else {
            _write_uint_root<uint64_t, NumDig>(x / Pow10<T>[32]);
            _write_uint<uint64_t, 16>(x % Pow10<T>[32] / Pow10<T>[16]);
            _write_uint<uint64_t, 16>(x % Pow10<T>[16]);
          }
          return;
        }

        if constexpr (NumDig < numeric_limits<T>::digits10) {
          if (x >= Pow10<T>[NumDig]) {
            _write_uint_root<T, NumDig + MAX_PRECOMPUTE_NUM_DIGIT>(x);
            return;
          }
        }
        _write_uint_top<T, MAX_PRECOMPUTE_NUM_DIGIT>(x / Pow10<T>[NumDig - MAX_PRECOMPUTE_NUM_DIGIT]);
        if constexpr (NumDig > MAX_PRECOMPUTE_NUM_DIGIT) _write_uint<T, NumDig - MAX_PRECOMPUTE_NUM_DIGIT>(x % Pow10<T>[NumDig - MAX_PRECOMPUTE_NUM_DIGIT]);
      }

      public:
      inline void flush() {
        fwrite(write_buffer.begin(), 1, write_ptr - write_buffer.begin(), stdout);
        write_ptr = write_buffer.begin();
      }

      FastIO& operator<<(const char& x) {
        write_commonop();
        putchar(x);
        return *this;
      }

      FastIO& operator<<(const string& x) {
        write_commonop();
        uint idx = 0;
        while (idx < x.size()) {
          const uint siz = min(BUFFER_SIZE - (uint) (write_ptr - write_buffer.begin()), (uint) x.size() - idx);
          copy_n(x.begin() + idx, siz, write_ptr);
          write_ptr += siz;
          idx += siz;
          if (write_ptr == write_buffer.end()) flush();
        }
        return *this;
      }

      template<typename T>
        requires(integral<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
      FastIO& operator<<(const T& x) {
        write_commonop();
        constexpr bool is_signed = is_signed_v<T> || is_same_v<T, __int128_t>;
        if constexpr (is_signed) {
          if (x < 0) {
            putchar('-');
            _write_uint_root<T, MAX_PRECOMPUTE_NUM_DIGIT>(-x);
            return *this;
          }
        }
        _write_uint_root<T, MAX_PRECOMPUTE_NUM_DIGIT>(x);
        return *this;
      }

      // ===== Common ====== //

      public:
      FastIO() {
        read_ptr = read_buffer.begin();
        write_ptr = write_buffer.begin();
        load();
      }

      ~FastIO() { flush(); }
    };
  }  // namespace _internal

  _internal::FastIO FastIO;
}  // namespace asalib
#line 10 "tests/matrix/core/libchecker-pow.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/pow_of_matrix"

using mint = asalib::ds::static_modint<998244353>;

int main() {
  int n;
  ll k;
  asalib::FastIO >> n >> k;
  asalib::matrix::Matrix<mint> A(n, n);
  rep(i, n) rep(j, n) {
    int x;
    asalib::FastIO >> x;
    A.at(i, j) = x;
  }
  auto B = A.pow(k);
  rep(i, n) rep(j, n) asalib::FastIO << B.at(i, j).val() << " \n"[j == n - 1];
  return 0;
}
Back to top page