結果

問題 No.3226 2×2行列累乗
コンテスト
ユーザー ジュ・ビオレ・グレイス
提出日時 2025-07-18 02:05:22
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
RE  
実行時間 -
コード長 852 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,454 ms
コンパイル使用メモリ 122,752 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-13 09:46:36
合計ジャッジ時間 5,467 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/linuxbrew/.linuxbrew/opt/dmd/include/dlang/dmd/core/checkedint.d(814): Warning: cannot inline function `core.checkedint.mulu!().mulu`
ulong mulu()(ulong x, uint y, ref bool overflow)
      ^

ソースコード

diff #
raw source code

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

void main() {
	long A, B, C, D, S, T, N, K;
	readfln("%d %d %d %d %d %d %d %d", A, B, C, D, S, T, N, K);
	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