#P7229. Finding x in Linear Modular Equation from Expression

    ID: 20433 Type: Default 1000ms 256MiB

Finding x in Linear Modular Equation from Expression

Finding x in Linear Modular Equation from Expression

Given an arithmetic expression \(A\) that simplifies to a linear form \(kx+b\), together with two integers \(P\) and \(M\), find the smallest non-negative integer \(x\) such that the remainder when \(A\) is divided by \(M\) is equal to \(P\). The expression \(A\) is a valid arithmetic expression consisting of numbers, the variable x, operators +, -, and * as well as parentheses. Each operator connects exactly two numbers or variables. Multiplication is explicitly denoted by * and expressions containing \(x\) are not multiplied directly with one another. It is guaranteed that after simplification, \(A\) can be represented in the form \(kx+b\).

Reminder: Use standard operator precedence and associativity rules when evaluating the expression.

inputFormat

The input consists of a single line containing the expression \(A\) as a string, followed by two integers \(P\) and \(M\) separated by spaces.

Example: 3*x+2 4 5

outputFormat

Output the smallest non-negative integer \(x\) that satisfies the congruence \((kx+b) \bmod M = P\).

sample

3*x+2 4 5
1