This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_product"
#include <iostream>
#include <vector>
#include "../math/semiring_matrix.cpp"
#include "../math/static_modint.cpp"
#include "../traits/num_ring.cpp"
#include "../utility/int_alias.cpp"
#include "../utility/rep.cpp"
using Fp = Modint998244353;
using Matrix = SemiRingMatrix<NumRing<Fp>>;
int main() {
int N, M, K;
std::cin >> N >> M >> K;
std::vector<std::vector<Fp>> A(N, std::vector<Fp>(M));
std::vector<std::vector<Fp>> B(M, std::vector<Fp>(K));
for (auto& v : A) {
for (auto& x : v) {
u32 t;
std::cin >> t;
x = Fp(t);
}
}
for (auto& v : B) {
for (auto& x : v) {
u32 t;
std::cin >> t;
x = Fp(t);
}
}
const auto C = Matrix(A) * Matrix(B);
for (const auto i : rep(0, N)) {
for (const auto j : rep(0, K)) {
std::cout << C(i, j) << " \n"[j + 1 == K];
}
}
}
#line 1 "test/semiring_matrix.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/matrix_product"
#include <iostream>
#include <vector>
#line 2 "math/semiring_matrix.cpp"
#include <array>
#include <cassert>
#include <initializer_list>
#line 2 "utility/int_alias.cpp"
#include <cstdint>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 2 "utility/rep.cpp"
#include <algorithm>
class Range {
struct Iter {
int itr;
constexpr Iter(const int pos) noexcept : itr(pos) {}
constexpr void operator++() noexcept { ++itr; }
constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
constexpr int operator*() const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr Range(const int first, const int last) noexcept : first(first), last(std::max(first, last)) {}
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); }
constexpr Range rep(const int n) noexcept { return Range(0, n); }
#line 8 "math/semiring_matrix.cpp"
template <class S> class SemiRingMatrix {
using T = typename S::Type;
using A = typename S::Sum;
using M = typename S::Product;
using Self = SemiRingMatrix;
std::vector<std::vector<T>> data;
public:
SemiRingMatrix() = default;
explicit SemiRingMatrix(const int h, const int w, const T& val = A::zero()) : data(h, std::vector<T>(w, val)) {}
SemiRingMatrix(const std::vector<std::vector<T>>& vec) : data(vec) {
for (const auto& v : data) assert(v.size() == width());
}
SemiRingMatrix(const std::initializer_list<std::initializer_list<T>>& list) {
data.reserve(list.size());
for (const auto& v : list) data.emplace_back(v.begin(), v.end());
for (const auto& v : data) assert(v.size() == width());
}
int height() const { return data.size(); }
int width() const { return data.empty() ? 0 : data[0].size(); }
T& operator()(const int i, const int j) {
assert(0 <= i and i < height());
assert(0 <= j and j < width());
return data[i][j];
}
const T& operator()(const int i, const int j) const {
assert(0 <= i and i < height());
assert(0 <= j and j < width());
return data[i][j];
}
Self operator+(const Self& other) const { return Self(*this) += other; }
Self& operator+=(const Self& other) {
assert(height() == other.height());
assert(width() == other.width());
for (const int i : rep(height()))
for (const int j : rep(width())) data[i][j] = A::operation(data[i][j], other.data[i][j]);
return *this;
}
Self operator*(const Self& other) const {
assert(width() == other.height());
Self ret(height(), other.width(), A::identity());
for (const int i : rep(height()))
for (const int k : rep(width()))
for (const int j : rep(other.width()))
ret.data[i][j] = A::operation(ret.data[i][j], M::operation(data[i][k], other.data[k][j]));
return ret;
}
Self operator*(const T& other) const { return Self(*this) *= other; }
Self& operator*=(const T& other) {
for (const int i : rep(height()))
for (const int j : rep(width())) data[i][j] = M::operation(data[i][j], other);
}
Self pow(u64 exp) const {
assert(height() == width());
Self ret(height(), width(), A::identity()), mult(*this);
for (const int i : rep(height())) ret.data[i][i] = M::identity();
for (; exp > 0; exp >>= 1) {
if (exp & 1) ret = ret * mult;
mult = mult * mult;
}
return ret;
}
};
#line 2 "math/static_modint.cpp"
#include <ostream>
#include <type_traits>
#line 3 "math/rem_euclid.cpp"
template <class T> constexpr T rem_euclid(T value, const T& mod) {
assert(mod > 0);
return (value %= mod) >= 0 ? value : value + mod;
}
#line 2 "math/totient.cpp"
template <class T> constexpr T totient(T x) {
T ret = x;
for (T i = 2; i * i <= x; ++i) {
if (x % i == 0) {
ret /= i;
ret *= i - 1;
while (x % i == 0) x /= i;
}
}
if (x > 1) {
ret /= x;
ret *= x - 1;
}
return ret;
}
#line 7 "math/static_modint.cpp"
template <u32 MOD, std::enable_if_t<((u32)1 <= MOD and MOD <= ((u32)1 << 31))>* = nullptr> class StaticModint {
using Self = StaticModint;
static inline constexpr u32 PHI = totient(MOD);
u32 v;
public:
static constexpr u32 mod() noexcept { return MOD; }
template <class T, std::enable_if_t<std::is_integral_v<T>>* = nullptr>
static constexpr T normalize(const T& x) noexcept {
return rem_euclid<std::common_type_t<T, i64>>(x, MOD);
}
constexpr StaticModint() noexcept : v(0) {}
template <class T> constexpr StaticModint(const T& x) noexcept : v(normalize(x)) {}
template <class T> static constexpr Self raw(const T& x) noexcept {
Self ret;
ret.v = x;
return ret;
}
constexpr u32 val() const noexcept { return v; }
constexpr Self neg() const noexcept { return raw(v == 0 ? 0 : MOD - v); }
constexpr Self inv() const noexcept { return pow(PHI - 1); }
constexpr Self pow(u64 exp) const noexcept {
Self ret(1), mult(*this);
for (; exp > 0; exp >>= 1) {
if (exp & 1) ret *= mult;
mult *= mult;
}
return ret;
}
constexpr Self operator-() const noexcept { return neg(); }
constexpr Self operator~() const noexcept { return inv(); }
constexpr Self operator+(const Self& rhs) const noexcept { return Self(*this) += rhs; }
constexpr Self& operator+=(const Self& rhs) noexcept {
if ((v += rhs.v) >= MOD) v -= MOD;
return *this;
}
constexpr Self operator-(const Self& rhs) const noexcept { return Self(*this) -= rhs; }
constexpr Self& operator-=(const Self& rhs) noexcept {
if (v < rhs.v) v += MOD;
v -= rhs.v;
return *this;
}
constexpr Self operator*(const Self& rhs) const noexcept { return Self(*this) *= rhs; }
constexpr Self& operator*=(const Self& rhs) noexcept {
v = (u64)v * rhs.v % MOD;
return *this;
}
constexpr Self operator/(const Self& rhs) const noexcept { return Self(*this) /= rhs; }
constexpr Self& operator/=(const Self& rhs) noexcept { return *this *= rhs.inv(); }
constexpr bool operator==(const Self& rhs) const noexcept { return v == rhs.v; }
constexpr bool operator!=(const Self& rhs) const noexcept { return v != rhs.v; }
friend std::ostream& operator<<(std::ostream& stream, const Self& rhs) { return stream << rhs.v; }
};
using Modint1000000007 = StaticModint<1000000007>;
using Modint998244353 = StaticModint<998244353>;
#line 2 "traits/sum_group.cpp"
template <class T> struct SumGroup {
using Type = T;
static constexpr T identity() { return T(0); }
static constexpr T operation(const T& l, const T& r) { return l + r; }
static constexpr T inverse(const T& x) { return -x; }
};
#line 2 "traits/product_monoid.cpp"
template <class T> struct ProductMonoid {
using Type = T;
static constexpr T identity() { return T(1); }
static constexpr T operation(const T& l, const T& r) { return l * r; }
};
#line 4 "traits/num_ring.cpp"
template <class T> struct NumRing {
using Type = T;
using Sum = SumGroup<T>;
using Product = ProductMonoid<T>;
};
#line 9 "test/semiring_matrix.test.cpp"
using Fp = Modint998244353;
using Matrix = SemiRingMatrix<NumRing<Fp>>;
int main() {
int N, M, K;
std::cin >> N >> M >> K;
std::vector<std::vector<Fp>> A(N, std::vector<Fp>(M));
std::vector<std::vector<Fp>> B(M, std::vector<Fp>(K));
for (auto& v : A) {
for (auto& x : v) {
u32 t;
std::cin >> t;
x = Fp(t);
}
}
for (auto& v : B) {
for (auto& x : v) {
u32 t;
std::cin >> t;
x = Fp(t);
}
}
const auto C = Matrix(A) * Matrix(B);
for (const auto i : rep(0, N)) {
for (const auto j : rep(0, K)) {
std::cout << C(i, j) << " \n"[j + 1 == K];
}
}
}