(declaim (optimize (speed 3) (safety 0))) (defun fib (n m) (labels ((rec (a b c p q) (cond ((zerop c) (mod b m)) ((evenp c) (rec a b (floor c 2) (mod (+ (* p p) (* q q)) m) (mod (+ (* 2 p q) (* q q)) m))) (t (rec (mod (+ (* b q) (* a q) (* a p)) m) (mod (+ (* b p) (* a q)) m) (1- c) p q))))) (rec 1 0 n 0 1))) (defun main (&rest argv) (declare (ignorable argv)) (let* ((n (read)) (m (read))) (format t "~D~%" (fib (1- n) m)))) (main)