Created
September 16, 2020 07:19
-
-
Save ryloric/0a0c38d1dcd0a7c24ebfe12e0fc8a302 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Spoj code : COINS | |
(defun memoize (fn) | |
(let ((cache (make-hash-table :test #'equal))) | |
#'(lambda (&rest args) | |
(multiple-value-bind | |
(result exists) | |
(gethash args cache) | |
(if exists | |
result | |
(setf (gethash args cache) | |
(apply fn args))))))) | |
(defun exchange-val (n) | |
(cond ((eq n 0) 0) | |
((eq n 1) 1) | |
((eq n 2) 2) | |
((eq n 3) 3) | |
((eq n 4) 4) | |
((eq n 5) 5) | |
((eq n 6) 6) | |
((eq n 7) 7) | |
((eq n 8) 8) | |
((eq n 9) 9) | |
((eq n 10) 10) | |
((eq n 11) 11) | |
((eq n 12) 13) | |
(t (+ (exchange-val (floor n 2)) | |
(exchange-val (floor n 3)) | |
(exchange-val (floor n 4)))))) | |
(setf (fdefinition 'exchange-val) (memoize #'exchange-val)) | |
(defun run () | |
(loop for line = (read-line *standard-input* nil :eof) | |
until (eq line :eof) | |
do (format t "~a~%" (exchange-val (parse-integer line))))) | |
(run) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment