結果

問題 No.10 +か×か
ユーザー mmmpppmmmppp
提出日時 2017-03-07 19:20:32
言語 Ruby
(3.2.2)
結果
TLE  
実行時間 -
コード長 719 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 11,324 KB
実行使用メモリ 29,080 KB
最終ジャッジ日時 2023-09-05 21:59:33
合計ジャッジ時間 6,922 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,148 KB
testcase_01 AC 83 ms
15,264 KB
testcase_02 AC 81 ms
15,136 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N,T,*A = $<.read.split.map &:to_i
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{
  break if culc(A, s) == T
  if (2..N).each{|j|
    partial_sum = culc(A[0, j], s[0, j - 1])
    if h[[j, partial_sum]]
      s = succ(s[0,j - 1]) + ?+*(N - j)
      break
    elsif s[-1] == ?*
      s[/\*+$/].size.times{|k|
        partial_sum = culc(A[0, N - j], s[0, N - j - 1])
        h[[N - j, partial_sum]] = partial_sum
      }
    end
  }
    s = succ s
  end
}
$><<s
0