結果

問題 No.281 門松と魔法(1)
ユーザー yuruhiyayuruhiya
提出日時 2020-10-31 11:33:25
言語 Crystal
(1.11.2)
結果
WA  
実行時間 -
コード長 901 bytes
コンパイル時間 19,488 ms
コンパイル使用メモリ 256,444 KB
実行使用メモリ 812,884 KB
最終ジャッジ日時 2023-09-13 12:34:38
合計ジャッジ時間 24,505 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,464 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,448 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,432 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,392 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,448 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 3 ms
4,384 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 WA -
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 RE -
testcase_32 AC 2 ms
4,428 KB
testcase_33 AC 3 ms
4,392 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 WA -
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 2 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
  else
    res = (a - b) // d + 1
    {a - res*d, 0i64}.max < b ? res : INF
  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
      {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_i64 }
ans = solve(a, b, c, d)
puts ans < INF ? ans : -1
0