結果

問題 No.265 数学のテスト
ユーザー lam6er
提出日時 2025-04-15 21:45:07
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,013 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 82,036 KB
実行使用メモリ 107,948 KB
最終ジャッジ日時 2025-04-15 21:46:05
合計ジャッジ時間 5,395 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30 RE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

def add_polynomials(a, b):
    result = defaultdict(int)
    for exp, coeff in a.items():
        result[exp] += coeff
    for exp, coeff in b.items():
        result[exp] += coeff
    return {exp: coeff for exp, coeff in result.items() if coeff != 0}

def multiply_polynomials(a, b):
    result = defaultdict(int)
    for exp1, coeff1 in a.items():
        for exp2, coeff2 in b.items():
            exp = exp1 + exp2
            result[exp] += coeff1 * coeff2
    return {exp: coeff for exp, coeff in result.items() if coeff != 0}

def compute_derivative(poly):
    derivative = defaultdict(int)
    for exp, coeff in poly.items():
        if exp >= 1:
            new_exp = exp - 1
            derivative[new_exp] += coeff * exp
    return {exp: coeff for exp, coeff in derivative.items() if coeff != 0}

def parse_expression(s):
    terms = []
    remaining = s
    while True:
        term, remaining = parse_term(remaining)
        terms.append(term)
        if not remaining.startswith('+'):
            break
        remaining = remaining[1:]
    result = defaultdict(int)
    for term in terms:
        for exp, coeff in term.items():
            result[exp] += coeff
    return {exp: coeff for exp, coeff in result.items() if coeff != 0}

def parse_term(s):
    factors = []
    remaining = s
    while True:
        factor, remaining = parse_factor(remaining)
        factors.append(factor)
        if not remaining.startswith('*'):
            break
        remaining = remaining[1:]
    if not factors:
        return defaultdict(int), remaining
    result = factors[0].copy()
    for factor in factors[1:]:
        temp = defaultdict(int)
        for exp1, coeff1 in result.items():
            for exp2, coeff2 in factor.items():
                exp = exp1 + exp2
                temp[exp] += coeff1 * coeff2
        result = {exp: coeff for exp, coeff in temp.items() if coeff != 0}
    return result, remaining

def parse_factor(s):
    if not s:
        return {}, ''
    if s[0] == 'd' and len(s) >= 2 and s[1] == '{':
        stack = 1
        i = 2
        while i < len(s) and stack > 0:
            if s[i] == '{':
                stack += 1
            elif s[i] == '}':
                stack -= 1
            i += 1
        inner_expr = s[2:i-1]
        inner_poly = parse_expression(inner_expr)
        derivative_poly = compute_derivative(inner_poly)
        remaining = s[i:]
        return derivative_poly, remaining
    elif s[0].isdigit():
        coeff = int(s[0])
        return {0: coeff}, s[1:]
    elif s[0] == 'x':
        return {1: 1}, s[1:]
    else:
        raise ValueError(f"Unexpected character: {s[0]}")

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

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