atcoder#ABC270G. [ABC270G] Sequence in mod P
[ABC270G] Sequence in mod P
Score : points
Problem Statement
There is a sequence defined by the following recurrence relation.
$X_i = \left\{ \begin{array}{ll} S & (i = 0)\\ (A X_{i-1}+B) \bmod P & (i \geq 1) \end{array} \right.$
Determine whether there is an such that . If it exists, find the smallest such . Here, denotes the remainder when is divided by (the least non-negative residue).
You are given test cases for each input file.
Constraints
- is a prime.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Each test case is in the following format:
Output
Print lines.
The -th line should contain the smallest such that for , or -1
if there is no such .
3
5 2 1 1 0
5 2 2 3 0
11 1 1 0 10
3
-1
10
For the first test case, we have , so the smallest such that is . For the second test case, we have , so there is no such that .