#P7229. [COCI 2015/2016 #3] SLON

[COCI 2015/2016 #3] SLON

Description

Little Q is very naughty at school.

He is always bored in class, and he always makes the classroom a mess. The teacher wants him to calm down, so the teacher gave him a very hard math problem.

The teacher gives Little Q an arithmetic expression AA, and integers PP and MM. Little Q needs to answer the following question:

Find the smallest non-negative integer xx such that the value of the expression AA (with xx substituted in) has remainder PP when divided by MM.

Note that every operator connects two numbers or variables. Every multiplication sign is explicit, and it is not allowed to multiply two subexpressions that both contain xx. All parentheses are valid, and there may be parentheses that contain only a single number or variable.

The problem guarantees that after simplification, the original expression can always be written as a linear expression in one variable of the form kx+bkx+b.

Input Format

The first line contains the expression AA.

The second line contains two positive integers PP and MM.

Output Format

The first line contains one positive integer xx.

5+3+x
9 10

1
20+3+x
0 5

2

Hint

Constraints

For 100%100\% of the testdata:

  • Let A|A| be the length of the string AA. Then 1A1051 \le |A| \le 10 ^ 5.
  • The expression AA contains only +\texttt{+}, -\texttt{-}, *\texttt{*}, (\texttt{(}, )\texttt{)}, x\texttt{x}, and 0\texttt{0} \sim 9\texttt{9}.
  • 0PM10 \le P \le M - 1.
  • 1M1061 \le M \le 10 ^ 6.

Notes

Translated from COCI 2015-2016 #3 D SLON, full score 120.

Translated by ChatGPT 5