結果

問題 No.281 門松と魔法(1)
ユーザー yuruhiyayuruhiya
提出日時 2020-10-31 11:44:05
言語 Crystal
(1.11.2)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 1,165 bytes
コンパイル時間 19,953 ms
コンパイル使用メモリ 256,072 KB
実行使用メモリ 4,472 KB
最終ジャッジ日時 2023-09-13 12:35:30
合計ジャッジ時間 22,198 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,428 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,428 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,388 KB
testcase_08 AC 3 ms
4,412 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,404 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,436 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,388 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 2 ms
4,400 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 3 ms
4,468 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 3 ms
4,416 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 3 ms
4,376 KB
testcase_44 AC 2 ms
4,388 KB
testcase_45 AC 3 ms
4,468 KB
testcase_46 AC 3 ms
4,472 KB
testcase_47 AC 2 ms
4,392 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,404 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 3 ms
4,432 KB
testcase_53 AC 3 ms
4,376 KB
testcase_54 AC 2 ms
4,428 KB
testcase_55 AC 3 ms
4,424 KB
testcase_56 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

INF = 10i64 ** 9

# a -> less than b
def calc(a, b, d)
  if a < b
    {0i64, a}
  else
    res = (a - b) // d + 1
    next_a = {a - res * d, 0i64}.max
    next_a < b ? {res, next_a} : {INF, next_a}
  end
end

def solve(a, b, c, d)
  a, b, c = [a, b, c].map { |i| {i, 0i64}.max }
  if [a, b, c].count { |i| i == 0 } >= 2
    INF
  elsif a == b
    {solve(a - d, b, c, d) + 1, solve(a, b - d, c, d) + 1}.min
  elsif a == c
    {solve(a - d, b, c, d) + 1, solve(a, b, c - d, d) + 1}.min
  elsif b == c
    {solve(a, b - d, c, d) + 1, solve(a, b, c - d, d) + 1}.min
  else
    if (b > a && b > c) || (b < a && b < c)
      0i64
    else
      v1, next_a = calc(a, b, d)
      v2, next_c = calc(c, b, d)
      v3, next_b = calc(b, {a, c}.min, d)
      {v1 + v2 + solve(next_a, b, next_c, d), v3 + solve(a, next_b, c, d)}.min
    end
  end
end

d, a, b, c = (1..4).map { read_line.to_i64 }
if d == 0
  puts (b > a && b > c && a != c) || (b < a && b < c && a != c) ? 0 : -1
else
  ans = solve(a, b, c, d)
  puts ans < INF ? ans : -1
end
0