Editorial for COCI '15 Contest 3 #4 Slon


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

The condition from the task is that the expression is a first degree polynomial of the form ax+b. Let f(x) = ax+b. If we calculate the expression at 0, we will see that f(0) = b. If we calculate it at 1, we will see that f(1) = a+b. This gives us values a and b. We are left to figure out how to calculate the minimal value of x such that ax+b \equiv p \pmod{m}. Given that m \le 10^6, we can iterate over all values of x until we find the minimal value that satisfies the upper equation. We are left to figure out how to calculate the expression at 0 and 1.

One of the ways we can do this is using the command eval in Python. Another way is to parse the expression from infix to postfix notation using the "shunting-yard" algorithm. Another approach is to convert the expression to postfix notation and then the addition, multiplication and subtraction is performed over polynomials with maximal degree of 1 and, finally, the same equation of the form ax+b \equiv p \pmod{m} is solved.


Comments

There are no comments at the moment.