結果

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

ソースコード

diff #

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

    def tokenize(s):
        tokens = []
        i = 0
        while i < len(s):
            c = s[i]
            if c.isdigit():
                tokens.append(('number', int(c)))
                i += 1
            elif c == 'x':
                tokens.append(('x',))
                i += 1
            elif c == 'd':
                tokens.append(('d',))
                i += 1
            elif c in ['+', '*', '{', '}']:
                tokens.append((c,))
                i += 1
            else:
                raise ValueError("Invalid character: {}".format(c))
        return tokens

    tokens = tokenize(S)

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

        def peek(self):
            if self.pos < len(self.tokens):
                return self.tokens[self.pos]
            else:
                return None

        def consume(self, expected_type=None):
            if self.pos >= len(self.tokens):
                return None
            token = self.tokens[self.pos]
            if expected_type is not None and token[0] != expected_type:
                raise SyntaxError(f"Expected {expected_type}, got {token[0]}")
            self.pos += 1
            return token

        def parse_expr(self):
            coeff = self.parse_term()
            while self.peek() and self.peek()[0] == '+':
                self.consume('+')
                term_coeff = self.parse_term()
                coeff = self.add(coeff, term_coeff)
            return coeff

        def parse_term(self):
            coeff = self.parse_factor()
            while self.peek() and self.peek()[0] == '*':
                self.consume('*')
                factor_coeff = self.parse_factor()
                coeff = self.multiply(coeff, factor_coeff)
            return coeff

        def parse_factor(self):
            token = self.peek()
            if not token:
                raise SyntaxError("Unexpected end of input")
            if token[0] == 'number':
                self.consume()
                value = token[1]
                return self.constant(value)
            elif token[0] == 'x':
                self.consume()
                return self.x()
            elif token[0] == 'd':
                self.consume('d')
                self.consume('{')
                expr_coeff = self.parse_expr()
                self.consume('}')
                return self.derivative(expr_coeff)
            else:
                raise SyntaxError(f"Unexpected token: {token[0]}")

        def constant(self, value):
            coeff = [0] * (self.d + 1)
            coeff[0] = value
            return coeff

        def x(self):
            coeff = [0] * (self.d + 1)
            if self.d >= 1:
                coeff[1] = 1
            return coeff

        def derivative(self, coeff):
            result = [0] * (self.d + 1)
            for k in range(self.d + 1):
                if k + 1 <= self.d:
                    result[k] = (k + 1) * coeff[k + 1]
                else:
                    result[k] = 0
            return result

        def add(self, a, b):
            return [a[i] + b[i] for i in range(self.d + 1)]

        def multiply(self, a, b):
            result = [0] * (self.d + 1)
            for i in range(self.d + 1):
                for j in range(self.d + 1):
                    if i + j <= self.d:
                        result[i + j] += a[i] * b[j]
            return result

    parser = Parser(tokens, d)
    try:
        coeff = parser.parse_expr()
    except SyntaxError as e:
        print("Syntax error:", e)
        return

    print(' '.join(map(str, coeff)))

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