結果

問題 No.10 +か×か
ユーザー mmmpppmmmppp
提出日時 2017-03-07 20:45:09
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 1,204 bytes
コンパイル時間 337 ms
コンパイル使用メモリ 11,228 KB
実行使用メモリ 19,708 KB
最終ジャッジ日時 2023-09-05 22:33:19
合計ジャッジ時間 7,521 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
15,140 KB
testcase_01 AC 79 ms
15,084 KB
testcase_02 RE -
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:48: warning: possibly useless use of + in void context
Syntax OK

ソースコード

diff #

N,T,*A = $<.read.split.map &:to_i
debug = false
h = {}
s = ?+*(N - 1)

def succ s
  if s[-1] == ?+
    s[0..-2] + ?*
  else
    succ(s[0..-2]) + ?+
  end
end

def culc a, s
  l = a.size-1
  sum = a[0]
  l.times{|i|
    case s[i]
    when ?+ then sum+=a[i+1]
    when ?* then sum*=a[i+1]
    end
  }
  sum
end

loop{
  c = culc A, s
  p [:top, s, h, c] if debug  ####
  break if c == T
  if c > T
    last_asterisk_index = s=~/\*[^*]*$/
    if A[last_asterisk_index + 1] > 1
      s = succ(s[0, last_asterisk_index + 1]) + ?+*(N - last_asterisk_index - 2)
      next
    end
  end
  b = (2..N).each{|j|
    partial_sum = culc(A[0, j], s[0, j - 1])
    if h[[j, partial_sum]]
      p [:before, s, j] if debug  ####
      s = succ(s[0, j - 1]) + ?+*(N - j)
      p [:after, s, j] if debug  ####
      break
    end
  }
  if s[-1] == ?*
    s[/\*+$/].size.times{|k|
      2 + 1 + 1 * 1 * 8
      partial_sum = culc(A[0, N - k - 1], s[0, N - k - 2])
      p [:centre_before, s, k, h] if debug  ####
      h[[N - k - 1, partial_sum]] = partial_sum
      p [:centre_after, s, k, h] if debug  ####
    }
  end
  
  s = succ s if b
}
p s, h if debug
$><<s
0