結果

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

ソースコード

diff #

class Polynomial:
    def __init__(self, coeffs):
        self.coeffs = coeffs

    def __repr__(self):
        return str(self.coeffs)

    def derivative(self):
        new_coeffs = []
        for i in range(1, len(self.coeffs)):
            new_coeffs.append(self.coeffs[i] * i)
        if not new_coeffs:
            return Polynomial([0])
        return Polynomial(new_coeffs)

    def __add__(self, other):
        max_len = max(len(self.coeffs), len(other.coeffs))
        new_coeffs = [0] * max_len
        for i in range(max_len):
            a = self.coeffs[i] if i < len(self.coeffs) else 0
            b = other.coeffs[i] if i < len(other.coeffs) else 0
            new_coeffs[i] = a + b
        return Polynomial(new_coeffs)

    def __mul__(self, other):
        new_coeffs = [0] * (len(self.coeffs) + len(other.coeffs) - 1)
        for i in range(len(self.coeffs)):
            for j in range(len(other.coeffs)):
                new_coeffs[i + j] += self.coeffs[i] * other.coeffs[j]
        return Polynomial(new_coeffs)

class Parser:
    def __init__(self, s):
        self.s = s
        self.pos = 0

    def parse_expression(self):
        terms = [self.parse_term()]
        while self.pos < len(self.s) and self.s[self.pos] == '+':
            self.pos += 1
            terms.append(self.parse_term())
        res = terms[0]
        for term in terms[1:]:
            res = res + term
        return res

    def parse_term(self):
        factors = [self.parse_factor()]
        while self.pos < len(self.s) and self.s[self.pos] == '*':
            self.pos += 1
            factors.append(self.parse_factor())
        res = factors[0]
        for factor in factors[1:]:
            res = res * factor
        return res

    def parse_factor(self):
        if self.pos >= len(self.s):
            raise Exception("Unexpected end of input")
        if self.s[self.pos].isdigit():
            num = int(self.s[self.pos])
            self.pos += 1
            return Polynomial([num])
        elif self.s[self.pos] == 'x':
            self.pos += 1
            return Polynomial([0, 1])
        elif self.s[self.pos] == 'd' and self.pos + 1 < len(self.s) and self.s[self.pos+1] == '{':
            self.pos += 2
            f = self.parse_expression()
            if self.pos >= len(self.s) or self.s[self.pos] != '}':
                raise Exception("Unclosed brace")
            self.pos += 1
            df = f.derivative()
            return df
        else:
            raise Exception(f"Unexpected character '{self.s[self.pos]}' at position {self.pos}")

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx +=1
    d = int(input[idx])
    idx +=1
    S = input[idx]
    idx +=1

    parser = Parser(S)
    poly = parser.parse_expression()
    coeffs = poly.coeffs.copy()
    while len(coeffs) < d+1:
        coeffs.append(0)
    coeffs = coeffs[:d+1]
    print(' '.join(map(str, coeffs)))

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