結果

問題 No.816 Beautiful tuples
ユーザー nao22b
提出日時 2019-04-19 22:00:37
言語 Elixir
(1.18.1)
結果
AC  
実行時間 630 ms / 1,500 ms
コード長 782 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 63,704 KB
実行使用メモリ 55,028 KB
最終ジャッジ日時 2024-12-31 02:49:20
合計ジャッジ時間 10,650 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

defmodule Main do

    def is_answer(a, b, _) when (a == b), do: false
    def is_answer(a, _, c) when (a == c), do: false
    def is_answer(_, b, c) when (b == c), do: false
    def is_answer(a, b, c) do
        r1 = rem(a + b, c)
        r2 = rem(a + c, b)
        r3 = rem(b + c, a)
        r1 == 0 && r2 == 0 && r3 == 0
    end

    def solve(a, b, n) do
        r = rem(a + b, n)
        d2 = div(a + b, n)
        cond do
            is_answer(a, b, a + b) -> (a + b)
            r == 0 && is_answer(a, b, d2) -> d2
            d2 < a - b || d2 < b - a -> -1
            true -> solve(a, b, n + 1)
        end
    end

    def main do
        [a, b] = IO.gets("") |> String.trim |> String.split |> Enum.map(&String.to_integer/1)
        solve(a, b, 1) |> IO.puts
    end
end

0