結果

問題 No.10 +か×か
ユーザー letrangerjpletrangerjp
提出日時 2017-05-14 17:06:35
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,416 ms / 5,000 ms
コード長 485 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 11,596 KB
実行使用メモリ 59,624 KB
最終ジャッジ日時 2023-08-21 06:21:55
合計ジャッジ時間 5,146 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
15,260 KB
testcase_01 AC 77 ms
15,376 KB
testcase_02 AC 77 ms
15,032 KB
testcase_03 AC 842 ms
47,056 KB
testcase_04 AC 97 ms
15,964 KB
testcase_05 AC 79 ms
15,160 KB
testcase_06 AC 1,416 ms
59,624 KB
testcase_07 AC 839 ms
37,200 KB
testcase_08 AC 129 ms
16,928 KB
testcase_09 AC 163 ms
18,788 KB
testcase_10 AC 80 ms
15,280 KB
testcase_11 AC 81 ms
15,116 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def f(a, total)
  r = []
  dp = {}
  g = lambda{|res, idx|
    return false if res > total
    return res == total if idx == a.size
    return false if dp[[res,idx]]
    dp[[res,idx]] = true

    [:+, :*].each{|op|
      new_res = res.send(op, a[idx])
      r << op
      if g.(new_res, idx+1)
        return true
      end
      r.pop
    }
    false
  }
  first = a.shift
  g.(first, 0)
  r
end

N = gets.to_i
Total = gets.to_i
A = gets.split.take(N).map(&:to_i)
puts f(A, Total)*""
0