A15388 | Coprime
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Given an array of $n$ positive integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 1000$ ). Find the maximum value of $i + j$ such that $a_i$ and $a_j$ are coprime, $^{\dagger}$ or $-1$ if no such $i$ , $j$ exist.
For example consider the array $[1, 3, 5, 2, 4, 7, 7]$ . The maximum value of $i + j$ that can be obtained is $5 + 7$ , since $a_5 = 4$ and $a_7 = 7$ are coprime.
$^{\dagger}$ Two integers $p$ and $q$ are [coprime](https://en.wikipedia.org/wiki/Coprime_integers) if the only positive integer that is a divisor of both of them is $1$ (that is, their [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) is $1$ ).
For example consider the array $[1, 3, 5, 2, 4, 7, 7]$ . The maximum value of $i + j$ that can be obtained is $5 + 7$ , since $a_5 = 4$ and $a_7 = 7$ are coprime.
$^{\dagger}$ Two integers $p$ and $q$ are [coprime](https://en.wikipedia.org/wiki/Coprime_integers) if the only positive integer that is a divisor of both of them is $1$ (that is, their [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) is $1$ ).
输入格式
The input consists of multiple test cases. The first line contains an integer $t$ ( $1 \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 $n$ ( $2 \leq n \leq 2\cdot10^5$ ) — the length of the array.
The following line contains $n$ space-separated positive integers $a_1$ , $a_2$ ,..., $a_n$ ( $1 \leq a_i \leq 1000$ ) — the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$ .
The first line of each test case contains an integer $n$ ( $2 \leq n \leq 2\cdot10^5$ ) — the length of the array.
The following line contains $n$ space-separated positive integers $a_1$ , $a_2$ ,..., $a_n$ ( $1 \leq a_i \leq 1000$ ) — the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot10^5$ .
输出格式
For each test case, output a single integer — the maximum value of $i + j$ such that $i$ and $j$ satisfy the condition that $a_i$ and $a_j$ are coprime, or output $-1$ in case no $i$ , $j$ satisfy the condition.
输入输出样例
输入 #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 = 3$ , with sum of indices equal to $6$ , since $1$ and $1$ are coprime.
For the second test case, we can choose $i = 7$ and $j = 5$ , with sum of indices equal to $7 + 5 = 12$ , since $7$ and $4$ are coprime.
For the second test case, we can choose $i = 7$ and $j = 5$ , with sum of indices equal to $7 + 5 = 12$ , since $7$ and $4$ are coprime.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted