#P3306. [SDOI2013] 随机数生成器
[SDOI2013] 随机数生成器
Description
Recently, Xiao W plans to read a new book that has pages, with page numbers ranging from .
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 denote the -th number generated by this method, i.e., the page Xiao W will read on day . This method requires three parameters , satisfying , and are integers. A sequence of integers is generated according to the following formula:
Here, 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 of this book, so he wants to know the earliest day on which he can read page , or determine that he will never read page .
Input Format
This problem contains multiple test cases in a single test point.
The first line contains an integer , the number of test cases.
Each of the next lines contains five integers , 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 . If he will never read page , output .
3
7 1 1 3 3
7 2 2 2 0
7 2 2 2 1
1
3
-1
Hint
Constraints
- .
- , .
- is a prime.
Translated by ChatGPT 5
京公网安备 11011102002149号