結果

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

ソースコード

diff #

def main():
    import sys
    N = int(sys.stdin.readline())
    d = int(sys.stdin.readline())
    S = sys.stdin.readline().strip()

    def parse_expression(s, pos, max_degree):
        terms = []
        while pos < len(s):
            term, pos = parse_term(s, pos, max_degree)
            terms.append(term)
            if pos < len(s) and s[pos] == '+':
                pos += 1
            else:
                break
        total = [0] * (max_degree + 1)
        for term in terms:
            for i in range(max_degree + 1):
                total[i] += term[i]
        return total, pos

    def parse_term(s, pos, max_degree):
        factors = []
        while pos < len(s):
            factor, pos = parse_factor(s, pos, max_degree)
            factors.append(factor)
            if pos < len(s) and s[pos] == '*':
                pos += 1
            else:
                break
        product = [0] * (max_degree + 1)
        product[0] = 1  # Initialize with 1 for multiplication
        for factor in factors:
            product = multiply_polynomials(product, factor, max_degree)
        return product, pos

    def parse_factor(s, pos, max_degree):
        if pos >= len(s):
            return [0] * (max_degree + 1), pos
        if s[pos] == 'x':
            return make_polynomial(1, 1, max_degree), pos + 1
        elif s[pos].isdigit():
            num = 0
            while pos < len(s) and s[pos].isdigit():
                num = num * 10 + int(s[pos])
                pos += 1
            return make_polynomial(0, num, max_degree), pos
        elif s[pos] == 'd':
            # Process derivative: d{...}
            pos += 2  # Skip 'd' and '{'
            expr, pos = parse_expression(s, pos, max_degree)
            derivative = compute_derivative(expr, max_degree)
            if pos < len(s) and s[pos] == '}':
                pos += 1
            else:
                pass  # Assuming valid input
            return derivative, pos
        else:
            return [0] * (max_degree + 1), pos

    def make_polynomial(degree, coefficient, max_degree):
        poly = [0] * (max_degree + 1)
        if degree <= max_degree:
            poly[degree] = coefficient
        return poly

    def compute_derivative(poly, max_degree):
        derivative = [0] * (max_degree + 1)
        for k in range(1, max_degree + 1):
            if k - 1 <= max_degree:
                derivative[k - 1] = poly[k] * k
        return derivative

    def multiply_polynomials(a, b, max_degree):
        result = [0] * (max_degree + 1)
        for i in range(max_degree + 1):
            if a[i] == 0:
                continue
            for j in range(max_degree + 1):
                if b[j] == 0:
                    continue
                k = i + j
                if k > max_degree:
                    continue
                result[k] += a[i] * b[j]
        return result

    poly, pos = parse_expression(S, 0, d)
    print(' '.join(map(str, poly)))

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