#abc270g. G - Sequence in mod P
G - Sequence in mod P
Score : points
问题描述
存在一个序列 ,由以下递推关系定义。
$X_i = \left\{ \begin{array}{ll} S & (i = 0)\\ (A \times X_{i-1}+B) \bmod P & (i \geq 1) \end{array} \right.$
确定是否存在某个 使得 。如果存在,找出最小的这样的 值。
此处, 表示 除以 后的余数(最小非负剩余数)。
对于每个输入文件,你将得到 个测试用例。
以上为通义千问 qwen-max 翻译,仅供参考。
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 .
Sample Input 1
3
5 2 1 1 0
5 2 2 3 0
11 1 1 0 10
Sample Output 1
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 .
update @ 2024/3/10 11:24:23