#P3306. [SDOI2013] 随机数生成器

    ID: 2355 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2013山东O2优化素数判断,质数,筛法最大公约数,gcd逆元

[SDOI2013] 随机数生成器

Description

Recently, Xiao W plans to read a new book that has pp pages, with page numbers ranging from 0p10 \sim p-1.

Xiao W is busy, so he can read only one page per day. To make things a bit more interesting, he plans to use the linear congruential method he learned at NOI2012 to generate a sequence to decide which page to read each day.

Let xix_i denote the ii-th number generated by this method, i.e., the page Xiao W will read on day ii. This method requires three parameters a,b,x1a, b, x_1, satisfying 0a,b,x1<p0 \leq a, b, x_1 \lt p, and a,b,x1a, b, x_1 are integers. A sequence of integers is generated according to the following formula:

xi+1a×xi+b(modp)x_{i+1} \equiv a \times x_i + b \pmod p

Here, mod\bmod denotes the remainder operation.

However, this method may cause the same page number to be read on two different days.

Xiao W wants to read page tt of this book, so he wants to know the earliest day on which he can read page tt, or determine that he will never read page tt.

Input Format

This problem contains multiple test cases in a single test point.

The first line contains an integer TT, the number of test cases.

Each of the next TT lines contains five integers p,a,b,x1,tp, a, b, x_1, t, representing one test case.

Output Format

For each test case, output one line with a single integer indicating the earliest day on which he reads page tt. If he will never read page tt, output 1-1.

3
7 1 1 3 3
7 2 2 2 0
7 2 2 2 1

1 
3 
-1

Hint

Constraints

  • 1T501 \leq T \leq 50.
  • 0a,b,x1,t<p0 \leq a, b, x_1, t \lt p, 2p1092 \leq p \leq 10^9.
  • pp is a prime.

Translated by ChatGPT 5