#P8993. [北大集训 2021] 算术

[北大集训 2021] 算术

Background

CTT2021 D4T1.

Problem Description

Today, Xiao Q, who lives in a base-1414 world, learned a method to determine whether a given big integer is a multiple of 99. We use (1BB40)14=(70812)10(1BB40)_{14} = (70812)_{10} as an example to describe this method. Let b=14b=14 and p=9p=9. All operations in the method below are performed in base bb.

  1. From the least significant digit to the most significant digit, split the number into blocks, each consisting of k=2k=2 consecutive digits. In the example, (1BB40)b(1BB40)_{b} is split into three blocks: 1BB401 \mid BB \mid 40.
  2. From the least significant block to the most significant block, number each block starting from 00. In the example, block 00 is 4040, block 11 is BBBB, and block 22 is 11.
  3. Compute a value bib_i for block ii: let the value of block ii in base bb be aia_i. If ii is odd, then bib_i is the smallest non-negative integer such that (ai+bi)0(modp)(a_i+b_i) \equiv 0 \pmod p. If ii is even, then bib_i is the smallest non-negative integer such that (aibi)0(modp)(a_i-b_i) \equiv 0 \pmod p. In the example, b0=2b_0=2, b1=6b_1=6, and b2=1b_2=1.
  4. Concatenate the bib_i values in order with larger indices in lower digits and smaller indices in higher digits, forming a base-bb number, and output it. In the example, the output is (261)b=(477)10(261)_{b} = (477)_{10}. It is easy to verify that both 477477 and 7081270812 are multiples of pp.

It can be proven that the input and output numbers produced by the method above are either both multiples of pp, or both not multiples of pp. Also, the number of digits becomes smaller, so by repeating this process several times you can obtain a very small number, and then you can check it easily.

Xiao Q was deeply attracted by this algorithm, so he wants to give a general method for b,pb,p different from 14,914,9. However, he found that when the values of b,pb,p change, kk in Step 1 is not necessarily 22: sometimes it is 11, sometimes it is greater than 22, and sometimes there is no kk that satisfies the requirement at all. Therefore, for given bb and pp, Xiao Q wants to know the minimum positive integer kk in Step 1 (in base bb), such that no matter what the input is, the input and the corresponding output are either both multiples of pp, or both not multiples of pp, or report that such a kk does not exist.

Note that pp is not necessarily a prime number.

Input Format

There are multiple test cases in the testdata. It is guaranteed that within the same test point, pp is the same. The first line contains two positive integers T,pT,p, with 1T1051 \le T \le 10^5 and 2p10152 \leq p \le 10^{15}, representing the number of test cases and the parameter pp of the method.

The next TT lines each contain one integer bb, representing the base for each test case. It is guaranteed that 2p<b10×p2 \leq p < b \leq 10 \times p.

All numbers in the input are given in decimal.

Output Format

For each test case, output one line. If no valid kk exists, output -1; otherwise, output the minimum positive integer kk that satisfies the condition.

2 9
14
16
2
-1

Hint

Subtask ID 2p2\leq p\leq 1T1\leq T\leq Score
11 33 1010 55
22 1010 55
33 10210^2 10210^2 55
44 10410^4 1111
55 10610^6 1111
66 10810^8 10310^3 1111
77 101010^{10} 1111
88 101210^{12} 77
99 101410^{14} 10410^4 1717
1010 101510^{15} 10510^5 1717

For the physical and mental health of contestants, the distributed file provides a big-integer modular multiplication function mul(A, B, P) implemented in down.cpp. You need to ensure A,B[0,P1]A,B \in [0,P-1], and the function will return (A×B)modP(A \times B) \bmod P. You may choose to use or not use this code freely. When calling it, you must ensure that A,B,PA,B,P are all no greater than 101510^{15}.

Translated by ChatGPT 5