結果

問題 No.3 ビットすごろく
ユーザー gemmarogemmaro
提出日時 2020-04-27 08:31:07
言語 Elixir
(1.16.2)
結果
TLE  
実行時間 -
コード長 855 bytes
コンパイル時間 1,232 ms
コンパイル使用メモリ 56,604 KB
実行使用メモリ 52,860 KB
最終ジャッジ日時 2023-08-30 00:43:42
合計ジャッジ時間 11,750 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 619 ms
49,140 KB
testcase_01 AC 614 ms
49,784 KB
testcase_02 AC 626 ms
49,152 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

defmodule Main do
  def main do
    {n, _} =
      IO.read(:line)
      |> Integer.parse()

    n
    |> solve()
    |> IO.puts()
  end

  def solve(n) do
    solve_rec([1], n)
  end

  def solve_rec([h | t], n) do
    c =
      h
      |> Integer.digits(2)
      |> Enum.sum()

    cond do
      h + c == n ->
        ([h | t] |> length) + 1

      true ->
        [
          if(h + c < n && !((h + c) in [h | t]),
            do: solve_rec([h + c | [h | t]], n),
            else: -1
          ),
          if(h - c > 1 && !((h - c) in [h | t]),
            do: solve_rec([h - c | [h | t]], n),
            else: -1
          )
        ]
        |> Enum.filter(&(&1 != -1))
        |> (fn l ->
              cond do
                l |> Enum.empty?() -> -1
                true -> l |> Enum.min()
              end
            end).()
    end
  end
end
0