#P15733. [JAG 2024 Summer Camp #2] Expression Sum

[JAG 2024 Summer Camp #2] Expression Sum

题目描述

You are given a string SS. Each character in SS is one of 0123456789+()\texttt{0123456789+()}?

Let TT be a string formed by replacing each ?\texttt{?} in SS with one of 0123456789+()\texttt{0123456789+()}. Define eval(T)\text{eval}(T) as follows:

  • If TT is a valid expression, then it is the value obtained by evaluating TT as an expression.
  • If TT is not a valid expression, then it is 00.

Compute the sum of eval(T)\text{eval}(T) for all possible ways to replace each ?\texttt{?} in SS with one of 0123456789+()\texttt{0123456789+()}, and output the result modulo 998,244,353998,244,353.

A valid expression is defined by the following BNF:

$$\begin{aligned} \texttt{} &\ ::= \ \texttt{} \ \texttt{"+"} \ \texttt{} \ | \ \texttt{} \\ \texttt{} &\ ::= \ \texttt{"("} \ \texttt{} \ \texttt{")"} \ | \ \texttt{} \\ \texttt{} &\ ::= \ \texttt{} \ \texttt{} \ | \ \texttt{} \\ \texttt{} &\ ::= \ \texttt{} \ \texttt{} \ | \ \texttt{} \\ \texttt{} &\ ::= \ \texttt{"0"} \ | \ \texttt{} \\ \texttt{} &\ ::= \ \texttt{"1"} \ | \ \texttt{"2"} \ | \ \texttt{"3"} \ | \ \texttt{"4"} \ | \ \texttt{"5"} \ | \ \texttt{"6"} \ | \ \texttt{"7"} \ | \ \texttt{"8"} \ | \ \texttt{"9"} \end{aligned} $$

输入格式

The input is given in the following format:

SS
  • 1S3,0001 \leq |S| \leq 3,000
  • Each character of SS is one of 0123456789+()\texttt{0123456789+()}?

输出格式

Output the answer.

?1?
46306
20???0+2??
651059511