結果

問題 No.281 門松と魔法(1)
ユーザー yuruhiyayuruhiya
提出日時 2020-10-31 11:41:01
言語 Crystal
(1.11.2)
結果
RE  
実行時間 -
コード長 1,069 bytes
コンパイル時間 20,636 ms
コンパイル使用メモリ 256,352 KB
実行使用メモリ 812,756 KB
最終ジャッジ日時 2023-09-13 12:35:08
合計ジャッジ時間 24,352 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 }
ans = solve(a, b, c, d)
puts ans < INF ? ans : -1
0