結果

問題 No.265 数学のテスト
ユーザー koba-e964koba-e964
提出日時 2016-12-15 15:54:19
言語 Ruby
(3.3.0)
結果
AC  
実行時間 614 ms / 2,000 ms
コード長 2,261 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 11,484 KB
実行使用メモリ 26,696 KB
最終ジャッジ日時 2023-08-16 02:10:19
合計ジャッジ時間 9,157 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 235 ms
18,852 KB
testcase_01 AC 253 ms
20,484 KB
testcase_02 AC 614 ms
26,696 KB
testcase_03 AC 292 ms
19,700 KB
testcase_04 AC 373 ms
22,488 KB
testcase_05 AC 453 ms
23,188 KB
testcase_06 AC 311 ms
19,688 KB
testcase_07 AC 387 ms
23,048 KB
testcase_08 AC 542 ms
25,536 KB
testcase_09 AC 344 ms
21,784 KB
testcase_10 AC 383 ms
21,996 KB
testcase_11 AC 409 ms
22,784 KB
testcase_12 AC 85 ms
15,084 KB
testcase_13 AC 124 ms
16,388 KB
testcase_14 AC 118 ms
15,944 KB
testcase_15 AC 80 ms
15,080 KB
testcase_16 AC 120 ms
15,948 KB
testcase_17 AC 79 ms
15,164 KB
testcase_18 AC 78 ms
15,304 KB
testcase_19 AC 81 ms
15,136 KB
testcase_20 AC 83 ms
15,320 KB
testcase_21 AC 78 ms
15,116 KB
testcase_22 AC 143 ms
16,720 KB
testcase_23 AC 79 ms
15,252 KB
testcase_24 AC 82 ms
15,004 KB
testcase_25 AC 193 ms
17,056 KB
testcase_26 AC 81 ms
15,084 KB
testcase_27 AC 78 ms
14,996 KB
testcase_28 AC 90 ms
15,224 KB
testcase_29 AC 78 ms
15,068 KB
testcase_30 AC 86 ms
15,260 KB
testcase_31 AC 78 ms
15,180 KB
testcase_32 AC 82 ms
15,064 KB
testcase_33 AC 80 ms
15,000 KB
testcase_34 AC 79 ms
15,084 KB
testcase_35 AC 79 ms
15,196 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:1: warning: assigned but unused variable - n
Main.rb:129: warning: assigned but unused variable - pos
Syntax OK

ソースコード

diff #

n=gets.to_i
$d=gets.to_i
$s=gets.chomp

$memo = Array.new($s.size, false)
$memo_mul = Array.new($s.size, false)

def parse(pos)
  if pos >= $s.size
    return nil
  end
  if $memo[pos] != false
    return $memo[pos]
  end
  res = nil
  nxt = nil
  res, nxt = parse_mul(pos)
  if res.nil?
    return $memo[pos]=nil
  end
  res2 = nil
  nxt2 = nil
  if $s[nxt] == '+'
    res2, nxt2 = parse(nxt + 1)
    if res2.nil?
      return $memo[pos]=nil
    end
    return $memo[pos] = [add(res, res2), nxt2]
  end
  return $memo[pos] = [res, nxt]
end
def parse_mul(pos)
  if pos >= $s.size
    return nil
  end
  if $memo_mul[pos] != false
    return $memo_mul[pos]
  end
  res = nil
  nxt = nil
  res, nxt = parse_simple(pos)
  if res.nil?
    return $memo_mul[pos]=nil
  end
  res2 = nil
  nxt2 = nil
  if $s[nxt] == '*'
    res2, nxt2 = parse_mul(nxt + 1)
    if res2.nil?
      return $memo_mul[pos]=nil
    end
    return $memo_mul[pos]=[mul(res, res2), nxt2]
  else
    return $memo_mul[pos]=[res, nxt]
  end
end

def parse_simple(pos)
  res, nxt = parse_d(pos)
  if res
    return [res, nxt]
  end
  res, nxt = parse_obj(pos)
  if res
    return [res, nxt]
  end
  nil
end

def parse_d(pos)
  if $s[pos] != 'd' || $s[pos + 1] != '{'
    return nil
  end
  res, nxt = parse(pos + 2)
  if res.nil? || $s[nxt] != '}'
    return nil
  end
  return [diff(res), nxt + 1]
end

def parse_obj(pos)
  if pos >= $s.size
    return nil
  end
  poly = Array.new($d + 1, 0)
  if '0' <= $s[pos] && $s[pos] <= '9'
    i = $s[pos].ord - 48
    poly[0] = i
    return [poly, pos + 1]
  end
  if $s[pos] == 'x'
    poly[1] = 1
    return [poly, pos + 1]
  end
  nil
end

def diff(poly)
  d = poly.size - 1
  res = Array.new(d + 1, 0)
  for i in 0 .. d - 1
    res[i] = (i + 1) * poly[i + 1]
  end
  res
end
def mul(poly1, poly2)
  d = poly1.size - 1
  res = Array.new(d + 1, 0)
  for i in 0 .. d
    for j in 0 .. d - i
      res[i + j] += poly1[i] * poly2[j]
    end
  end
  res
end

def add(poly1, poly2)
  d = poly1.size - 1
  res = Array.new(d + 1, 0)
  for i in 0 .. d
    res[i] += poly1[i] + poly2[i]
  end
  res
end

for i in 0 ... $s.size
  parse($s.size - i - 1)
end
result, pos = parse(0)
for i in 0 .. $d
  print(result[i])
  print(if i == $d then "\n" else " " end)
end
0