Created
April 15, 2012 10:35
-
-
Save mnzk/2391793 to your computer and use it in GitHub Desktop.
atcoder-q001-b.clj
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
;; AtCoder 過去問 : 001-B 【リモコン】 | |
;; http://arc001.contest.atcoder.jp/tasks/arc001_2 | |
(defn- q001-b* | |
[A B] | |
(letfn [(loop- [a b d acc xs] | |
(cond | |
(zero? d) acc | |
(empty? xs) (+ acc d) | |
:else (let [[x & xs] xs | |
times (quot d x) | |
times' (inc times) | |
next-a (+ a (* x times)) | |
next-a' (+ next-a x) | |
next-d (- b next-a) | |
next-d' (- next-a' b)] | |
(min (loop- next-a b next-d (+ acc times) xs) | |
(loop- b next-a' next-d' (+ acc times') xs)))))] | |
(if (< A B) | |
(loop- A B (- B A) 0 [10 5]) | |
(loop- B A (- A B) 0 [10 5])))) | |
(defn q001-b | |
[] | |
(let [xs (-> (read-line) | |
(clojure.string/split ,,, #"\s+")) | |
[A B] (map #(Long/parseLong %) xs)] | |
(println (q001-b* A B)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment