結果
| 問題 | No.3226 2×2行列累乗 |
| ユーザー |
ジュ・ビオレ・グレイス
|
| 提出日時 | 2025-07-19 20:02:41 |
| 言語 | D (dmd 2.113.0) |
| 結果 |
AC
不安定
|
| 実行時間 | 1 ms / 2,000 ms |
| + 353µs | |
| コード長 | 1,018 bytes |
| 記録 | |
| コンパイル時間 | 2,596 ms |
| コンパイル使用メモリ | 123,132 KB |
| 実行使用メモリ | 9,868 KB |
| 最終ジャッジ日時 | 2026-11-11 05:42:51 |
| 合計ジャッジ時間 | 4,246 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
ソースコード
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);
}
ジュ・ビオレ・グレイス