Guest User

Untitled

a guest
Feb 16th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns testhere.core
  2.   (:use [clojure.core.match :only [match]]))
  3.  
  4. (defn div [a b]
  5.   (if (>= (* a b) 0)
  6.     (/ a b)
  7.     (+ (/ a b) 1)))
  8.  
  9. (defn gcd [a b]
  10.   (loop [a a, b b, x 0, y 1, lx 1, ly 0]
  11.     (if (zero? b)
  12.       [a lx ly]
  13.       (let [q (div a b)]
  14.         (recur b (rem a b)
  15.                (- lx (* q x))
  16.                (- ly (* q y))
  17.                x y)))))
  18.  
  19. (defn diof-solve [a b c]
  20.   (letfn [(divs-by-a? [x] (zero? (rem x a)))
  21.           (divs-by-b? [x] (zero? (rem x b)))]
  22.     (match [a b c                   ]
  23.            [0 0 0                   ] [true 0 0 0]
  24.            [0 0 _                   ] [false 0 0 0]
  25.            [0 _ (c :when divs-by-b?)] [true 0 (/ c b) 0]
  26.            [0 _ _                   ] [false 0 0 0]
  27.            [_ 0 (c :when divs-by-a?)] [true (/ c a) 0 0]
  28.            [_ 0 _                   ] [false 0 0 0]
  29.            [_ _ _                   ] (let [[divisor x y] (gcd a b)]
  30.                                         (if (zero? (rem c divisor))
  31.                                           [true
  32.                                            (/ (* x c) divisor)
  33.                                            (/ (* y c) divisor)
  34.                                            divisor]
  35.                                           [false 0 0 0])))))
  36.  
  37. (defn solve-task [rx ry a b]
  38.   (let [[solvable da db dc] (diof-solve a b (- ry rx))
  39.         solve (fn []
  40.                 (let [min-infinity Integer/MIN_VALUE
  41.                       infinity Integer/MAX_VALUE
  42.                       same-sign? (fn [x y] (> (* x y) 0))
  43.                       diff-sign? (complement same-sign?)
  44.  
  45.                       kl (if (not (zero? (rem (* da dc) b)))
  46.                            (inc (/ (* (- da) dc)
  47.                                    b))
  48.                            (/ (* (- da) dc)
  49.                               b))
  50.                       kh (if (not (zero? (rem (* db dc) a)))
  51.                            (inc (/ (* db dc)
  52.                                    a))
  53.                            (/ (* db dc)
  54.                               a))
  55.  
  56.                       kkl (max (if (same-sign? b dc) kl min-infinity)
  57.                                (if (same-sign? a dc) kh min-infinity))
  58.                       kkh (min (if (diff-sign? b dc) kl infinity)
  59.                                (if (diff-sign? a dc) kh infinity))]
  60.  
  61.                   (cond
  62.                    (= kkl min-infinity) (+ da (/ (* kkh b) dc)
  63.                                            (- db) (/ (* kkh a) dc))
  64.  
  65.                    (= kkh infinity) (+ da (/ (* kkl b) dc)
  66.                                        (- db) (/ (* kkl a) dc))
  67.  
  68.                    :else (min
  69.                           (+ da (/ (* kkh b) dc)
  70.                              (- db) (/ (* kkh a) dc))
  71.                           (+ da (/ (* kkl b) dc)
  72.                              (- db) (/ (* kkl a) dc))))))]
  73.     (max -1 (cond (not solvable) -1
  74.                   (= a 0) (- db)
  75.                   (= b 0) da
  76.                   :else (solve)))))
Advertisement
Add Comment
Please, Sign In to add comment