トヨタ自動車プログラミングコンテスト2022(AtCoder Beginner Contest 270)
G
Sequence in mod P
AtCoder
⌀ 2220
原题
← Ex→
翻译中…

Problem Statement

翻译中…

There is a sequence $X=(X_0, X_1, \ldots)$ 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 $i$ such that $X_i=G$. If it exists, find the smallest such $i$. Here, $x \bmod y$ denotes the remainder when $x$ is divided by $y$ (the least non-negative residue).

翻译中…

You are given $T$ test cases for each input file.

翻译中…

Constraints

翻译中…
  • $1 \leq T \leq 100$
  • $2 \leq P \leq 10^9$
  • $P$ is a prime.
  • $0\leq A,B,S,G < P$
  • All values in the input are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

翻译中…

Each test case is in the following format:

翻译中…

P A B S G

翻译中…

Output

翻译中…

Print $T$ lines. The $t$-th line should contain the smallest $i$ such that $X_i=G$ for $\mathrm{case}_t$, or -1 if there is no such $i$.

翻译中…

样例

Input #1
3
5 2 1 1 0
5 2 2 3 0
11 1 1 0 10
Output #1
3
-1
10
For the first test case, we have $X=(1,3,2,0,\ldots)$, so the smallest $i$ such that $X_i=0$ is $3$. For the second test case, we have $X=(3,3,3,3,\ldots)$, so there is no $i$ such that $X_i=0$.
题目信息
题号
abc270_g
时间限制
4 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。