結果

問題 No.3226 2×2行列累乗
ユーザー ジュ・ビオレ・グレイス
提出日時 2025-07-18 02:07:44
言語 D
(dmd 2.109.1)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 909 bytes
コンパイル時間 1,514 ms
コンパイル使用メモリ 130,880 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-07-19 21:45:39
合計ジャッジ時間 5,501 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.algorithm, std.array, std.conv, std.typecons;

void main() {
	auto nums = readln.split.to!(long[]);
	auto A = nums[0], B = nums[1], C = nums[2], D = nums[3], S = nums[4], T= nums[5], N = nums[6], K = nums[7];
	auto mat = Matrix(A%K, B%K, C%K, D%K);
	
	// calculate M^N
	auto matN = Matrix(1, 0, 0, 1);
	auto mat_pow = mat;
	auto pow = 1UL;
	while (pow <= N) {
		if ((pow & N) != 0) matN = multiply(mat_pow, matN, K);
		pow *= 2;
		mat_pow = multiply(mat_pow, mat_pow, K);
	}
	
	auto R = matN[0]*S + matN[1]*T, U = matN[2]*S + matN[3]*T;
	auto R2 = R % K, U2 = U % K;
	if (R2 < 0) R2 += K;
	if (U2 < 0) U2 += K;
	
	writefln("%d %d", R2, U2);
}

// 0 1
// 2 3
alias Matrix = Tuple!(long, long, long, long);

Matrix multiply(Matrix a, Matrix b, long K) {
	return Matrix( (a[0]*b[0] + a[1]*b[2]) % K, (a[0]*b[1] + a[1]*b[3]) % K, (a[2]*b[0] + a[3]*b[2]) % K, (a[2]*b[1] + a[3]*b[3]) % K);
}
0