CF1742D.Coprime

普及-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Given an array of nn positive integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤10001 \le a_i \le 1000). Find the maximum value of i+ji + j such that aia_i and aja_j are coprime,†^{\dagger} or −1-1 if no such ii, jj exist.

For example consider the array [1,3,5,2,4,7,7][1, 3, 5, 2, 4, 7, 7]. The maximum value of i+ji + j that can be obtained is 5+75 + 7, since a5=4a_5 = 4 and a7=7a_7 = 7 are coprime.

†^{\dagger} Two integers pp and qq are coprime if the only positive integer that is a divisor of both of them is 11 (that is, their greatest common divisor is 11).

给定一个包含 nn 个正整数的数组 a1,a2,…,ana_1, a_2, \dots, a_n(其中 1≤ai≤10001 \le a_i \le 1000)。请找出满足 aia_i 与 aja_j 互质†^{\dagger} 的下标对 (i,j)(i, j) 中 i+ji + j 的最大值;若不存在这样的下标对,则返回 −1-1。

例如,考虑数组 [1,3,5,2,4,7,7][1, 3, 5, 2, 4, 7, 7]。所能得到的最大 i+ji + j 值为 5+75 + 7,因为 a5=4a_5 = 4 与 a7=7a_7 = 7 互质。

†^{\dagger} 两个整数 pp 和 qq 被称为互质,当且仅当能同时整除它们的唯一正整数是 11(即它们的最大公约数为 11)。

输入格式

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤101 \leq t \leq 10) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤2⋅1052 \leq n \leq 2\cdot10^5) — the length of the array.

The following line contains nn space-separated positive integers a1a_1, a2a_2,..., ana_n (1≤ai≤10001 \leq a_i \leq 1000) — the elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot10^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤101 \leq t \leq 10),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2\cdot10^5),表示数组的长度。

接下来的一行包含 nn 个以空格分隔的正整数 a1a_1, a2a_2, ..., ana_n(1≤ai≤10001 \leq a_i \leq 1000),即数组的元素。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, output a single integer — the maximum value of i+ji + j such that ii and jj satisfy the condition that aia_i and aja_j are coprime, or output −1-1 in case no ii, jj satisfy the condition.

对于每个测试用例,输出一个整数——满足条件 aia_i 与 aja_j 互质的所有下标对 (i,j)(i, j) 中 i+ji + j 的最大值;若不存在满足条件的 ii、jj,则输出 −1-1。

输入输出样例

  • 输入#1

    6
    3
    3 2 1
    7
    1 3 5 2 4 7 7
    5
    1 2 3 4 5
    3
    2 2 4
    6
    5 4 3 15 12 16
    5
    1 2 2 3 6

    输出#1

    6
    12
    9
    -1
    10
    7

说明/提示

For the first test case, we can choose i=j=3i = j = 3, with sum of indices equal to 66, since 11 and 11 are coprime.

For the second test case, we can choose i=7i = 7 and j=5j = 5, with sum of indices equal to 7+5=127 + 5 = 12, since 77 and 44 are coprime.

对于第一个测试用例,我们可以选择 i=j=3i = j = 3,此时下标之和为 66,因为 11 和 11 互质。

对于第二个测试用例,我们可以选择 i=7i = 7 和 j=5j = 5,此时下标之和为 7+5=127 + 5 = 12,因为 77 和 44 互质。

输入解题思路,AI测评打分。不知道怎么写?

首页