結果

問題 No.532 Possible or Impossible
ユーザー cympfhcympfh
提出日時 2017-06-30 17:24:06
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 463 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 19,484 KB
最終ジャッジ日時 2024-04-15 05:46:19
合計ジャッジ時間 4,947 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
19,232 KB
testcase_01 AC 79 ms
12,288 KB
testcase_02 AC 78 ms
12,288 KB
testcase_03 AC 78 ms
12,160 KB
testcase_04 AC 78 ms
12,288 KB
testcase_05 AC 78 ms
12,160 KB
testcase_06 AC 84 ms
12,288 KB
testcase_07 AC 78 ms
12,160 KB
testcase_08 AC 78 ms
12,160 KB
testcase_09 AC 77 ms
12,160 KB
testcase_10 AC 80 ms
12,288 KB
testcase_11 AC 77 ms
12,288 KB
testcase_12 AC 76 ms
12,160 KB
testcase_13 AC 133 ms
12,288 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def dfs(xs, goal)

  if xs.size == 1
    x = xs[0]
    return (goal == x)
  end

  x = xs.shift
  y = xs.shift
  can = []
  can << x + y
  can << x - y
  can << y - x
  can << x * y
  can << Rational(y, x) if x != 0
  can << Rational(x, y) if y != 0

  can.each do |z|
    return true if dfs([z] + xs, goal)
  end

  false
end

def solve(n, goal)
  a = (1..n).to_a
  dfs a, goal
end

m, n = gets.chomp.split.map(&:to_i)
puts solve(m, n) ? :Possible : :Impossible
0