QOJ.ac

QOJ

Límite de tiempo: 2.0 s Límite de memoria: 512 MB Puntuación total: 100 Hackeable ✓

#18136. 公用生成元

Estadísticas

對於兩個整數 $x$ 與 $y$ ($x, y \ge 2$),若 $x$ 可以透過執行以下操作若干次(可能為零次)後變為 $y$,則稱 $x$ 為 $y$ 的生成元(generator):

  • 選擇 $x$ 的一個因數 $d$ ($d \ge 2$),然後將 $x$ 增加 $d$。

例如:

  • $3$ 是 $8$ 的生成元,因為我們可以進行以下操作:$3 \xrightarrow{d=3} 6 \xrightarrow{d=2} 8$;
  • $4$ 是 $10$ 的生成元,因為我們可以進行以下操作:$4 \xrightarrow{d=4} 8 \xrightarrow{d=2} 10$;
  • $5$ 不是 $6$ 的生成元,因為我們無法透過上述操作將 $5$ 變為 $6$。

現在,Kevin 給你一個包含 $n$ 個兩兩相異整數的陣列 $a$ ($a_i \ge 2$)。 你需要找到一個整數 $x \ge 2$,使得對於每個 $1 \le i \le n$,$x$ 都是 $a_i$ 的生成元,或者判斷這樣的整數不存在。

輸入格式

每個測試包含多個測試案例。輸入的第一行包含一個單一整數 $t$ ($1 \le t \le 10^4$),代表測試案例的數量。接著是各測試案例的描述。

每個測試案例的第一行包含一個單一整數 $n$ ($1 \le n \le 10^5$),代表陣列 $a$ 的長度。

第二行包含 $n$ 個整數 $a_1, a_2, \dots, a_n$ ($2 \le a_i \le 4 \cdot 10^5$),代表陣列 $a$ 中的元素。保證元素兩兩相異。

保證所有測試案例的 $n$ 之總和不超過 $10^5$。

輸出格式

對於每個測試案例,輸出一個單一整數 $x$ —— 即你找到的整數。如果不存在有效的 $x$,則輸出 $-1$。

若有多個答案,你可以輸出其中任意一個。

範例

輸入格式 1

4
3
8 9 10
4
2 3 4 5
2
147 154
5
3 6 8 25 100000

輸出格式 1

2
-1
7
3

說明

在第一個測試案例中,對於 $x = 2$:

  • $2$ 是 $8$ 的生成元,因為我們可以進行以下操作:$2 \xrightarrow{d=2} 4 \xrightarrow{d=4} 8$;
  • $2$ 是 $9$ 的生成元,因為我們可以進行以下操作:$2 \xrightarrow{d=2} 4 \xrightarrow{d=2} 6 \xrightarrow{d=3} 9$;
  • $2$ 是 $10$ 的生成元,因為我們可以進行以下操作:$2 \xrightarrow{d=2} 4 \xrightarrow{d=2} 6 \xrightarrow{d=2} 8 \xrightarrow{d=2} 10$。

在第二個測試案例中,可以證明無法找到這四個整數的共同生成元。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.