結果

問題 No.281 門松と魔法(1)
コンテスト
ユーザー yuruhiya
提出日時 2020-10-31 10:46:27
言語 Crystal
(1.19.1)
コンパイル:
crystal build -Donline_judge -o a.out --release --no-debug _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 838 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 9,173 ms
コンパイル使用メモリ 336,112 KB
実行使用メモリ 1,289,704 KB
最終ジャッジ日時 2026-03-21 07:51:50
合計ジャッジ時間 13,111 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 42 WA * 9 RE * 4 TLE * 1 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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

# a -> less than b
def calc(a, b, d)
  if a < b
    0
  else
    (a - b) // d + 1
  end
end

def solve(a, b, c, d)
  a, b, c = [a, b, c].map { |i| {i, 0}.max }
  if [a, b, c].count { |i| i == 0 } >= 2
    Int32::MAX
  elsif a == b
    {1 + solve(a - d, b, c, d), 1 + solve(a, b - d, c, d)}.min
  elsif a == c
    {1 + solve(a - d, b, c, d), 1 + solve(a, b, c - d, d)}.min
  elsif b == c
    {1 + solve(a, b - d, c, d), 1 + solve(a, b, c - d, d)}.min
  else
    if (b > a && b > c) || (b < a && b < c)
      0
    else
      {calc(a, b, d) + calc(c, b, d), calc(b, {a, c}.min, d)}.min
    end
  end
end

d, a, b, c = (1..4).map { read_line.to_i }
ans = solve(a, b, c, d)
puts ans < Int32::MAX ? ans : -1
0