E - Last 9 Digits Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800800

問題文

nnn^n10910^9 で割った余りが XX となるような正の整数 nn が存在するか判定し、存在する場合は最小のものを求めてください。

QQ 個のテストケースが与えられるので、それぞれに対して答えてください。

制約

  • 1Q100001 \leq Q \leq 10000
  • 1X10911 \leq X \leq 10^9 - 1
  • XX22 の倍数でも 55 の倍数でもない
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられます。ただし、ii 個目 (1iQ)(1 \leq i \leq Q) のテストケースにおける XX の値を XiX_i とします。

QQ
X1X_1
X2X_2
\vdots
XQX_Q

出力

QQ 行にわたって出力してください。ii 行目には、ii 個目のテストケースに対する答えを出力してください。ただし、条件を満たす nn が存在しない場合は答えを 1-1 とします。


入力例 1Copy

Copy
2
27
311670611

出力例 1Copy

Copy
3
11

この入力例は 22 個のテストケースからなります。

  • 11 個目:33=273^3 = 2710910^9 で割った余りは 2727 なので、n=3n = 3 で条件を満たします。
  • 22 個目:1111=28531167061111^{11} = 28531167061110910^9 で割った余りは 311670611311670611 なので、n=11n = 11 で条件を満たします。

Score: 800800 points

Problem Statement

Determine whether there is a positive integer nn such that the remainder of nnn^n divided by 10910^9 is XX. If it exists, find the smallest such nn.

You will be given QQ test cases to solve.

Constraints

  • 1Q100001 \leq Q \leq 10000
  • 1X10911 \leq X \leq 10^9 - 1
  • XX is neither a multiple of 22 nor a multiple of 55.
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where XiX_i is the value of XX in the ii-th test case (1iQ)(1 \leq i \leq Q):

QQ
X1X_1
X2X_2
\vdots
XQX_Q

Output

Print QQ lines. The ii-th line should contain the answer for the ii-th test case. If no nn satisfies the condition, the answer should be 1-1.


Sample Input 1Copy

Copy
2
27
311670611

Sample Output 1Copy

Copy
3
11

This sample input consists of two test cases.

  • The first case: The remainder of 33=273^3 = 27 divided by 10910^9 is 2727, so n=3n = 3 satisfies the condition.
  • The second case: The remainder of 1111=28531167061111^{11} = 285311670611 divided by 10910^9 is 311670611311670611, so n=11n = 11 satisfies the condition.


2025-03-29 (Sat)
20:15:25 +00:00