結果

問題 No.3224 2×2行列入門
ユーザー mihhiael
提出日時 2025-08-09 13:23:11
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,153 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 100,500 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-08-09 13:23:13
合計ジャッジ時間 1,931 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <array>
#include <cassert>
#include <cmath>
#include <cstddef>

using namespace std;
using ll = long long;

ll isqrt(const ll x) {
	assert(x >= 0);
	ll rx = (ll)sqrt(x) - 5;
	while((rx + 1) * (rx + 1) <= x) rx++;
	return rx;
}

ll divceil(const ll a, const ll b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template<class T, size_t H, size_t W>
using mat = array<array<T, W>, H>;

template<class T, size_t H1, size_t W1, size_t H2, size_t W2>
auto mul(const mat<T, H1, W1> &a, const mat<T, H2, W2> &b) {
	static_assert(W1 == H2, "W1 must be equal to H2");
	mat<T, H1, W2> res{};
	for(size_t i = 0; i < H1; i++) {
		for(size_t j = 0; j < W2; j++) {
			for(size_t k = 0; k < W1; k++) {
				res[i][j] += a[i][k] * b[k][j];
			}
		}
	}
	return res;
}

#include <iostream>

using mat22 = mat<ll, 2, 2>;

int main() {
	mat22 m, n;
	for(int i=0;i<2;i++) {
		for(int j=0;j<2;j++){
			cin >> m[i][j];
		}
	}
	for(int i=0;i<2;i++) {
		for(int j=0;j<2;j++){
			cin >> n[i][j];
		}
	}
	const auto mn = mul(m, n);
	const auto mnmn = mul(mn, mn);
	for(int i = 0;i <2;i++) {
		for(int j = 0;j<2;j++) {
			cout << mnmn[i][j] << " ";
		}
		cout << "\n";
	}
}
0