結果

問題 No.10 +か×か
コンテスト
ユーザー letrangerjp
提出日時 2017-05-14 17:06:35
言語 Ruby
(4.0.0)
結果
AC  
実行時間 1,577 ms / 5,000 ms
コード長 485 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 68 ms
コンパイル使用メモリ 8,064 KB
実行使用メモリ 58,756 KB
最終ジャッジ日時 2025-12-06 14:36:00
合計ジャッジ時間 5,368 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #
raw source code

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