結果

問題 No.2441 行列累乗
コンテスト
ユーザー Lisp_Coder
提出日時 2024-05-16 11:05:01
言語 Common Lisp
(sbcl 2.6.3)
コンパイル:
sbclc _filename_
実行:
sbcl --script Main.fasl
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 874 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 67 ms
コンパイル使用メモリ 30,208 KB
実行使用メモリ 22,400 KB
最終ジャッジ日時 2026-05-27 11:47:30
合計ジャッジ時間 1,403 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 27 MAY 2026 11:47:27 AM):

; wrote /home/judge/data/code/Main.fasl
; compilation finished in 0:00:00.005

ソースコード

diff #
raw source code

(defun matrix-multiply (m1 m2)
  "Multiplies two 2x2 matrices."
  (let* ((a1 (first (first m1))) (b1 (second (first m1)))
         (c1 (first (second m1))) (d1 (second (second m1)))
         (a2 (first (first m2))) (b2 (second (first m2)))
         (c2 (first (second m2))) (d2 (second (second m2))))
    (list (list (+ (* a1 a2) (* b1 c2)) (+ (* a1 b2) (* b1 d2)))
          (list (+ (* c1 a2) (* d1 c2)) (+ (* c1 b2) (* d1 d2))))))

(defun matrix-power (matrix n)
  "Raises a 2x2 matrix to the nth power."
  (if (= n 1)
      matrix
      (matrix-multiply matrix (matrix-power matrix (1- n)))))

(defun main ()
  (let* ((matrix (list (list (read) (read)) (list (read) (read))))  ; 例として 1 2
                                                 ;          3 4 の行列を使用
         (result (matrix-power matrix 3)))
    (format t "~{~{~a ~}~%~}" result)))

(main)
0