#P10079. [GDKOI2024 普及组] EX 中缀表达式
[GDKOI2024 普及组] EX 中缀表达式
Problem Description
Alice has recently been learning about expressions, and now he wants to write his own expression calculator.
Rules:
- A single number: formed by concatenating several digits (at least one), and it must end with a
.character (without quotes). For example,00123.0.789.are all valid numbers. - A single operator: formed by concatenating several digits, and then ending with one character.
- The operator character: represents the operation of this operator. This character must be one of
+,*,^, representing addition, multiplication, and exponentiation, respectively. It is specially defined that0^0=1.
For example,000000789+,123^. If the preceding numeric value is valid, then they are valid operators. - The digits of an operator: represent the precedence of this operator. The precedence is a positive integer in . A larger number means higher precedence.
- For operators with the same precedence, the problem provides a string of length to indicate whether operators of that precedence are left-associative or right-associative.
Here,0means left-associative, and1means right-associative.
For example, if , then the -th character is0, meaning operators with precedence are left-associative. - Left-associative: means the operator is evaluated from left to right. Example:
1.4+2.4^3.4^4.is equivalent to((1.4+2.)4^3.)4^4., and its result is the same as((1+2)^3)^4. - Right-associative: means the operator is evaluated from right to left. Example:
1.6+2.6^3.6^4.is equivalent to1.6+(2.6^(3.6^4.)), and its result is the same as1+(2^(3^4)). - Infix expression:
- A single number is a valid infix expression.
- If is a valid infix expression, then is also a valid infix expression.
- If and are both valid infix expressions, and is a valid single operator, then is a valid infix expression.
- All other cases are invalid.
Now a string of length is given to specify, for operators with the same precedence, whether they are left-associative or right-associative.
Given an infix expression, determine whether it is valid. If it is invalid, output error (without quotes). If it is valid, output the value of the expression modulo .
Input Format
The first line contains a positive integer .
The second line contains a string of length .
The third line contains a string , representing an infix expression.
It is guaranteed that there are no spaces in the expression.
Output Format
If the infix expression is invalid, output error. Otherwise, output the value of the expression modulo .
2
01
1.2+2.1^3.2*4.2^(5.2*6.)2+7.
243640717
Hint
Sample Explanation
243640717 = ((1+2)^(3*(4^((5*6)+7)))) mod 998244353
Constraints
Special Property 1: The ^ character will not appear.
Special Property 2: The + and * characters will not appear.
Special Property 3: The ( and ) characters will not appear.
For of the testdata, , , and Special Property and Special Property hold.
For another of the testdata, , , and Special Property holds.
For another of the testdata, , , and Special Property and Special Property hold.
For another of the testdata, , , and Special Property holds.
For of the testdata, , .
Translated by ChatGPT 5