結果

問題 No.265 数学のテスト
ユーザー koba-e964koba-e964
提出日時 2016-12-15 15:24:29
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 1,725 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 7,296 KB
実行使用メモリ 26,624 KB
最終ジャッジ日時 2024-05-07 14:52:43
合計ジャッジ時間 4,995 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 AC 170 ms
26,624 KB
testcase_03 AC 123 ms
13,312 KB
testcase_04 AC 127 ms
13,184 KB
testcase_05 AC 137 ms
13,312 KB
testcase_06 AC 126 ms
13,312 KB
testcase_07 AC 133 ms
13,312 KB
testcase_08 AC 148 ms
13,184 KB
testcase_09 AC 122 ms
13,184 KB
testcase_10 AC 122 ms
13,312 KB
testcase_11 AC 133 ms
13,440 KB
testcase_12 AC 81 ms
12,160 KB
testcase_13 AC 94 ms
12,672 KB
testcase_14 AC 91 ms
12,928 KB
testcase_15 AC 79 ms
12,160 KB
testcase_16 AC 88 ms
12,672 KB
testcase_17 AC 74 ms
12,288 KB
testcase_18 AC 79 ms
12,160 KB
testcase_19 AC 77 ms
12,032 KB
testcase_20 AC 81 ms
12,160 KB
testcase_21 AC 79 ms
11,904 KB
testcase_22 AC 95 ms
12,672 KB
testcase_23 AC 78 ms
12,160 KB
testcase_24 AC 77 ms
11,904 KB
testcase_25 AC 104 ms
12,672 KB
testcase_26 AC 78 ms
12,288 KB
testcase_27 AC 78 ms
12,032 KB
testcase_28 AC 86 ms
12,544 KB
testcase_29 AC 79 ms
11,904 KB
testcase_30 AC 85 ms
12,160 KB
testcase_31 AC 80 ms
12,160 KB
testcase_32 AC 79 ms
12,032 KB
testcase_33 AC 80 ms
12,288 KB
testcase_34 AC 79 ms
12,032 KB
testcase_35 AC 78 ms
12,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:1: warning: assigned but unused variable - n
Main.rb:104: warning: assigned but unused variable - pos
Syntax OK

ソースコード

diff #

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

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

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

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

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

def diff(poly)
  res = []
  for p, q in poly
    if q >= 1
      res << [p * q, q - 1]
    end
  end
  res
end
def mul(poly1, poly2)
  res = []
  for p1, q1 in poly1
    for p2, q2 in poly2
      res << [p1 * p2, q1 + q2]
    end
  end
  res
end

result, pos = parse(s, 0)
poly = Array.new(d + 1, 0)
for p, q in result
  poly[q] += p
end
for i in 0 .. d
  print(poly[i])
  print(if i == d then "\n" else " " end)
end
0