結果

問題 No.10 +か×か
ユーザー らっしー(raccy)らっしー(raccy)
提出日時 2015-10-21 23:46:46
言語 Ruby
(3.3.0)
結果
AC  
実行時間 116 ms / 5,000 ms
コード長 1,354 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 11,280 KB
実行使用メモリ 16,892 KB
最終ジャッジ日時 2023-08-21 06:15:19
合計ジャッジ時間 2,086 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,176 KB
testcase_01 AC 80 ms
15,140 KB
testcase_02 AC 79 ms
15,096 KB
testcase_03 AC 116 ms
16,892 KB
testcase_04 AC 83 ms
15,036 KB
testcase_05 AC 81 ms
15,100 KB
testcase_06 AC 105 ms
16,268 KB
testcase_07 AC 83 ms
15,224 KB
testcase_08 AC 82 ms
15,156 KB
testcase_09 AC 81 ms
15,308 KB
testcase_10 AC 83 ms
15,268 KB
testcase_11 AC 81 ms
15,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class NiseOCR
  def initialize(list)
    @list = list
    @minmax_list = list.inject([[1, 0]]) do |accs, i|
      accs << [
        [accs[-1][0] + i, accs[-1][0] * i].min,
        [accs[-1][1] + i, accs[-1][1] * i].max,
      ]
    end
    @memory = {}
  end

  def calc(total, point)
    pair = [total, point]
    return @memory[pair] if @memory.include?(pair)
    kaito_list = []

    if point == 0
      if total == @list[point]
        return ''
      else
        @memory[pair] = nil
        return
      end
    end

    # *
    if total % @list[point] == 0
      next_total = total / @list[point]
      if @minmax_list[point][0] <= next_total &&
          next_total <= @minmax_list[point][1]
        if (next_calc = calc(next_total, point - 1))
          kaito_list << next_calc + '*'
        end
      end
    end

    # +
    next_total = total - @list[point]
    if @minmax_list[point][0] <= next_total &&
        next_total <= @minmax_list[point][1]
      if (next_calc = calc(next_total, point - 1))
        kaito_list << next_calc + '+'
      end
    end
    if kaito_list.empty?
      @memory[pair] = nil
      return
    else
      kaito = kaito_list.max
      @memory[pair] = kaito
      return kaito
    end
  end
end

n = gets.to_i
total = gets.to_i
list = gets.split.map(&:to_i)
nocr = NiseOCR.new(list)

puts nocr.calc(total, n - 1)
0