結果
| 問題 |
No.3226 2×2行列累乗
|
| コンテスト | |
| ユーザー |
ジュ・ビオレ・グレイス
|
| 提出日時 | 2025-07-19 20:02:41 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,018 bytes |
| コンパイル時間 | 1,763 ms |
| コンパイル使用メモリ | 130,948 KB |
| 実行使用メモリ | 7,716 KB |
| 最終ジャッジ日時 | 2025-07-19 21:45:33 |
| 合計ジャッジ時間 | 4,671 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
import std.stdio, std.algorithm, std.array, std.conv, std.typecons;
void main() {
auto AB = readln.split.to!(long[]); auto A = AB[0], B = AB[1];
auto CD = readln.split.to!(long[]); auto C = CD[0], D = CD[1];
auto ST = readln.split.to!(long[]); auto S = ST[0], T = ST[1];
auto NK = readln.split.to!(long[]); auto N = NK[0], K = NK[1];
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);
}
ジュ・ビオレ・グレイス