結果

問題 No.2441 行列累乗
ユーザー Today03Today03
提出日時 2023-08-25 21:23:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 919 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 205,152 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-25 21:23:04
合計ジャッジ時間 2,965 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <class T>
using matrix = vector<vector<T>>;
template <class T>
matrix<T> operator*(const matrix<T>& a, const matrix<T>& b) {
	int n = a.size();
	matrix<T> ret(n, vector<T>(n));
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < n; k++) {
				ret[i][j] += a[i][k] * b[k][j];
			}
		}
	}
	return ret;
}
template <class T>
matrix<T> operator^(matrix<T> a, long long b) {
	int n = a.size();
	matrix<T> ret(n, vector<T>(n));
	for (int i = 0; i < n; i++) {
		ret[i][i] = 1;
	}
	while (b) {
		if (b & 1) {
			ret = ret * a;
		}
		a = a * a;
		b >>= 1;
	}
	return ret;
}

int main() {
	matrix<int> m(2, vector<int>(2));
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cin >> m[i][j];
		}
	}
	m = m ^ 3;
	for (int i = 0; i < 2; i++) {
		for (int j = 0; j < 2; j++) {
			cout << m[i][j] << ' ';
		}
		cout << endl;
	}
}
0