結果

問題 No.2441 行列累乗
ユーザー a01sa01toa01sa01to
提出日時 2024-04-01 00:48:49
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 11,237 bytes
コンパイル時間 3,366 ms
コンパイル使用メモリ 249,872 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-01 00:49:06
合計ジャッジ時間 5,250 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.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\\data_structure\\modint.hpp"

#line 4 "library\\data_structure\\modint.hpp"
#include <concepts>
#line 7 "library\\data_structure\\modint.hpp"
#include <type_traits>
using namespace std;

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

#line 4 "library\\math\\extgcd.hpp"
#include <optional>
#line 7 "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 11 "library\\data_structure\\modint.hpp"

namespace asalib {

  namespace ds {
    class modint_base {};

    template<typename T>
    concept is_modint = is_base_of_v<modint_base, T>;

    template<typename T>
    concept integral_or_modint = integral<T> || is_modint<T>;

    template<unsigned int mod, enable_if_t<(1 <= mod)>* = nullptr>
    class static_modint: private modint_base {
      using mint = static_modint;

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

      template<integral T>
      constexpr static_modint(T x): _val(((long long) x % mod + mod) % mod) {};

      friend constexpr mint operator+(const mint& l, const mint& r) { return mint(l._val + r._val); }
      friend constexpr mint operator-(const mint& l, const mint& r) { return mint(l._val + mod - r._val); }
      friend constexpr mint operator*(const mint& l, const mint& r) { return mint((long long) l._val * r._val); }
      friend constexpr mint operator/(const mint& l, const mint& r) { return l * r.inv(); }
      constexpr mint operator+() const { return *this; }
      constexpr mint operator-() const { return 0 - *this; }

      constexpr mint& operator+=(const mint& other) { return *this = *this + other; }
      constexpr mint& operator-=(const mint& other) { return *this = *this - other; }
      constexpr mint& operator*=(const mint& other) { return *this = *this * other; }
      constexpr mint& operator/=(const mint& other) { return *this = *this / other; }

      constexpr mint& operator++() { return *this += 1; }
      constexpr mint& operator--() { return *this -= 1; }
      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 modint_base {
      using mint = dynamic_modint;

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

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

      friend constexpr mint operator+(const mint& l, const mint& r) { return mint(l._val + r._val); }
      friend constexpr mint operator-(const mint& l, const mint& r) { return mint(l._val + l._mod - r._val); }
      friend constexpr mint operator*(const mint& l, const mint& r) { return mint((long long) l._val * r._val); }
      friend constexpr mint operator/(const mint& l, const mint& r) { return l * r.inv(); }
      constexpr mint operator+() const { return *this; }
      constexpr mint operator-() const { return 0 - *this; }

      constexpr mint& operator+=(const mint& other) { return *this = *this + other; }
      constexpr mint& operator-=(const mint& other) { return *this = *this - other; }
      constexpr mint& operator*=(const mint& other) { return *this = *this * other; }
      constexpr mint& operator/=(const mint& other) { return *this = *this / other; }

      constexpr mint& operator++() { return *this += 1; }
      constexpr mint& operator--() { return *this -= 1; }
      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(asalib::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 7 "library\\data_structure\\matrix.hpp"

namespace asalib {
  namespace ds {
    namespace internal {
      template<typename T>
      concept matval = integral_or_modint<T> || floating_point<T>;
    }

    template<internal::matval 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->row(i) * x.col(j)).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[std::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;
    };
  }  // namespace ds
}  // namespace asalib
#line 8 "main.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/2441"

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  asalib::ds::Matrix<ll> a(2, 2);
  rep(i, 2) rep(j, 2) cin >> a.at(i, j);
  auto ans = a.pow(3);
  rep(i, 2) {
    rep(j, 2) cout << ans.at(i, j) << " \n"[j == 1];
  }
  return 0;
}
0