結果

問題 No.265 数学のテスト
ユーザー gew1fw
提出日時 2025-06-12 16:31:48
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,939 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 81,312 KB
最終ジャッジ日時 2025-06-12 16:32:03
合計ジャッジ時間 4,900 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import defaultdict

def add_poly(a, b):
    res = defaultdict(int)
    for deg in a:
        res[deg] += a[deg]
    for deg in b:
        res[deg] += b[deg]
    return res

def multiply_poly(a, b):
    res = defaultdict(int)
    for deg_a, coeff_a in a.items():
        for deg_b, coeff_b in b.items():
            res[deg_a + deg_b] += coeff_a * coeff_b
    return res

def derivative(poly):
    res = defaultdict(int)
    for deg, coeff in poly.items():
        if deg == 0:
            continue
        res[deg - 1] += coeff * deg
    return res

def parse_expression(s, i):
    poly, i = parse_term(s, i)
    while i < len(s) and s[i] == '+':
        i += 1
        term_poly, i = parse_term(s, i)
        poly = add_poly(poly, term_poly)
    return poly, i

def parse_term(s, i):
    poly, i = parse_factor(s, i)
    while i < len(s) and s[i] == '*':
        i += 1
        factor_poly, i = parse_factor(s, i)
        poly = multiply_poly(poly, factor_poly)
    return poly, i

def parse_factor(s, i):
    if s[i] == 'd' and i + 1 < len(s) and s[i+1] == '{':
        i += 2
        expr_poly, i = parse_expression(s, i)
        if i >= len(s) or s[i] != '}':
            raise ValueError("Missing closing }")
        i += 1
        derived = derivative(expr_poly)
        return derived, i
    elif s[i] == 'x':
        poly = defaultdict(int)
        poly[1] = 1
        return poly, i + 1
    elif s[i].isdigit():
        num = int(s[i])
        poly = defaultdict(int)
        poly[0] = num
        return poly, i + 1
    else:
        raise ValueError(f"Unexpected character at {i}: {s[i]}")

def main():
    input = sys.stdin.read().split()
    N = int(input[0])
    d = int(input[1])
    S = input[2]
    poly, _ = parse_expression(S, 0)
    result = []
    for degree in range(d + 1):
        result.append(str(poly.get(degree, 0)))
    print(' '.join(result))

if __name__ == '__main__':
    main()
0