#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 , and integers and . Little Q needs to answer the following question:
Find the smallest non-negative integer such that the value of the expression (with substituted in) has remainder when divided by .
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 . 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 .
Input Format
The first line contains the expression .
The second line contains two positive integers and .
Output Format
The first line contains one positive integer .
5+3+x
9 10
1
20+3+x
0 5
2
Hint
Constraints
For of the testdata:
- Let be the length of the string . Then .
- The expression contains only , , , , , , and .
- .
- .
Notes
Translated from COCI 2015-2016 #3 D SLON, full score 120.
Translated by ChatGPT 5
京公网安备 11011102002149号