Algorithm-Library

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

View the Project on GitHub UScuber/Algorithm-Library

:heavy_check_mark: test/yosupo/Matrix/Matrix-Product.test.cpp

Depends on

Code

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

#include "../../../template/template.hpp"

#include "../../../math/mint.hpp"
#include "../../../math/matrix.hpp"

using mint = Mint<998244353>;

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n,m,k;
  cin >> n >> m >> k;
  matrix<mint> a(n, m), b(m, k);
  rep(i, n) rep(j, m) cin >> a[i][j];
  rep(i, m) rep(j, k) cin >> b[i][j];
  a *= b;
  rep(i, n){
    rep(j, k) cout << a[i][j] << " ";
    cout << "\n";
  }
}
#line 1 "test/yosupo/Matrix/Matrix-Product.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_product"

#line 1 "template/template.hpp"
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#include <cstring>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 1000000007;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
#line 4 "test/yosupo/Matrix/Matrix-Product.test.cpp"

#line 2 "math/mint.hpp"

template <int mod>
struct Mint {
  ll x;
  constexpr Mint(ll x = 0) : x((x + mod) % mod){}
  static constexpr int get_mod(){ return mod; }
  constexpr Mint operator-() const{ return Mint(-x); }
  constexpr Mint operator+=(const Mint &a){
    if((x += a.x) >= mod) x -= mod;
    return *this;
  }
  constexpr Mint &operator++(){
    if(++x == mod) x = 0;
    return *this;
  }
  constexpr Mint operator++(int){
    Mint temp = *this;
    if(++x == mod) x = 0;
    return temp;
  }
  constexpr Mint &operator-=(const Mint &a){
    if((x -= a.x) < 0) x += mod;
    return *this;
  }
  constexpr Mint &operator--(){
    if(--x < 0) x += mod;
    return *this;
  }
  constexpr Mint operator--(int){
    Mint temp = *this;
    if(--x < 0) x += mod;
    return temp;
  }
  constexpr Mint &operator*=(const Mint &a){
    (x *= a.x) %= mod;
    return *this;
  }
  constexpr Mint operator+(const Mint &a) const{ return Mint(*this) += a; }
  constexpr Mint operator-(const Mint &a) const{ return Mint(*this) -= a; }
  constexpr Mint operator*(const Mint &a) const{ return Mint(*this) *= a; }
  constexpr Mint pow(ll t) const{
    if(!t) return 1;
    Mint res = 1, v = *this;
    while(t){
      if(t & 1) res *= v;
      v *= v;
      t >>= 1;
    }
    return res;
  }
  constexpr Mint inv() const{ return pow(mod - 2); }
  constexpr Mint &operator/=(const Mint &a){ return (*this) *= a.inv(); }
  constexpr Mint operator/(const Mint &a) const{ return Mint(*this) /= a; }
  constexpr bool operator==(const Mint &a) const{ return x == a.x; }
  constexpr bool operator!=(const Mint &a) const{ return x != a.x; }
  constexpr bool operator<(const Mint &a) const{ return x < a.x; }
  constexpr bool operator>(const Mint &a) const{ return x > a.x; }
  friend istream &operator>>(istream &is, Mint &a){ return is >> a.x; }
  friend ostream &operator<<(ostream &os, const Mint &a){ return os << a.x; }
};
//using mint = Mint<1000000007>;
#line 1 "math/matrix.hpp"
template <class T>
struct matrix {
  int n,m;
  private:
  vector<vector<T>> a;
  public:
  matrix(const int n) : n(n), m(n), a(n, vector<T>(n)){}
  matrix(const int n, const int m) : n(n), m(m), a(n, vector<T>(m)){}
  matrix(const vector<vector<T>> &d) : a(d), n(d.size()), m(d[0].size()){}
  vector<T> &operator[](const int i){ return a[i]; }
  matrix &operator*=(const matrix &b){
    assert(m == b.n);
    vector<vector<T>> c(n, vector<T>(b.m));
    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++)
    for(int k = 0; k < b.m; k++){
      c[i][k] += a[i][j] * b.a[j][k];
    }
    a = c;
    return *this;
  }
  matrix &operator+=(const matrix &b){
    assert(n == b.n && m == b.m);
    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++)
      a[i][j] += b.a[i][j];
    return *this;
  }
  matrix &operator-=(const matrix &b){
    assert(n == b.n && m == b.m);
    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++)
      a[i][j] -= b.a[i][j];
    return *this;
  }
  matrix operator*(const matrix &b) const{ return matrix(*this) *= b; }
  matrix operator+(const matrix &b) const{ return matrix(*this) += b; }
  matrix operator-(const matrix &b) const{ return matrix(*this) -= b; }
  matrix pow(ll t) const{
    assert(n == m);
    matrix<T> b(n), c = *this;
    for(int i = 0; i < n; i++) b[i][i] = 1;
    while(t > 0){
      if(t & 1) b *= c;
      c *= c;
      t >>= 1;
    }
    return b;
  }
  T det() const{
    assert(n == m);
    matrix b = *this;
    T res(1);
    bool flip = false;
    for(int i = 0; i < n; i++){
      for(int j = i + 1; j < n; j++){
        while(b[j][i] > 0){
          swap(b[i], b[j]);
          flip ^= 1;
          const T d = b[j][i] / b[i][i];
          for(int k = i; k < n; k++){
            b[j][k] -= b[i][k] * d;
          }
        }
      }
      if(b[i][i] == 0) return 0;
      res *= b[i][i];
    }
    if(flip) res = -res;
    return res;
  }
  matrix inv(){
    assert(n == m);
    matrix b(n), c = *this;
    for(int i = 0; i < n; i++) b[i][i] = 1;
    int r = 0;
    for(int i = 0; i < n && r < n; i++){
      if(c[r][i] == 0){
        T max_val = 0; int mx_pos;
        for(int j = r+1; j < n; j++){
          if(max_val < c[j][i]) max_val = c[j][i], mx_pos = j;
        }
        if(max_val == 0) return false;
        swap(c[r], c[mx_pos]); swap(b[r], b[mx_pos]);
      }     
      T d = T(1) / c[r][i];
      for(int j = 0; j < n; j++) c[r][j] *= d, b[r][j] *= d;
      for(int j = 0; j < n; j++){
        T v = c[j][i];
        if(j == r || c[j][i] == 0) continue;
        for(int k = 0; k < n; k++){
          c[j][k] -= c[r][k] * v;
          b[j][k] -= b[r][k] * v;
        }
      }
      r++;
    }
    return b;
  }
  int size() const{ return n; }
  void debug(){
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++) cerr << a[i][j] << " ";
      cerr << "\n";
    }
  }
};
#line 7 "test/yosupo/Matrix/Matrix-Product.test.cpp"

using mint = Mint<998244353>;

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n,m,k;
  cin >> n >> m >> k;
  matrix<mint> a(n, m), b(m, k);
  rep(i, n) rep(j, m) cin >> a[i][j];
  rep(i, m) rep(j, k) cin >> b[i][j];
  a *= b;
  rep(i, n){
    rep(j, k) cout << a[i][j] << " ";
    cout << "\n";
  }
}
Back to top page