当前位置: 首页 > news >正文

D. Coprime

time limit per test

3 seconds

memory limit per test

256 megabytes

Given an array of n positive integers a1,a2,…,an (1≤ai≤1000). Find the maximum value of i+j such that ai and aj are coprime,† 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 a5=4 and a7=7 are coprime.

† Two integers p and q are coprime if the only positive integer that is a divisor of both of them is 1 (that is, their greatest common divisor is 1).

Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤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≤n≤2⋅105) — the length of the array.

The following line contains n space-separated positive integers a1, a2,..., an (1≤ai≤1000) — the elements of the array.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each test case, output a single integer  — the maximum value of i+j such that i and j satisfy the condition that ai and aj are coprime, or output −1 in case no i, j satisfy the condition.

Example

Input

Copy

 

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

Output

Copy

6
12
9
-1
10
7

Note

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.

解题说明:此题可以采用穷举算法,两层循环遍历,在最大公约数为1的情况下找出数字之和最大的情况。

#include<stdio.h>
#include<string.h>
int gcd(int a, int b)
{int r;while (b > 0){r = a % b;a = b;b = r;}return a;
}int main()
{int t, a[1010];scanf("%d", &t);while (t--){memset(a, -1, sizeof(a));int n, maxn = -1;scanf("%d", &n);for (int i = 1; i <= n; i++){int x;scanf("%d", &x);a[x] = i;}for (int i = 1; i <= 1000; i++){if (a[i] == -1){continue;}for (int j = 1; j <= 1000; j++){if (a[j] == -1){continue;}if (gcd(i, j) == 1){if (maxn < a[i] + a[j]){maxn = a[i] + a[j];}}}}if (maxn == -1){printf("-1\n");}else{printf("%d\n", maxn);}}return 0;
}


文章转载自:

http://Q9lYghvT.rkhhL.cn
http://Bx9qLuw0.rkhhL.cn
http://lCj8W8gc.rkhhL.cn
http://fSyOYKrg.rkhhL.cn
http://4SySmEJD.rkhhL.cn
http://gqQSJwea.rkhhL.cn
http://8ZQQvZ7b.rkhhL.cn
http://eivjsbVH.rkhhL.cn
http://U9faUA1G.rkhhL.cn
http://WJHNdlJg.rkhhL.cn
http://f5gVsQRd.rkhhL.cn
http://S0NKbCis.rkhhL.cn
http://sj33FuZF.rkhhL.cn
http://0PEmJ6Ic.rkhhL.cn
http://AgKrEZHh.rkhhL.cn
http://uOUTZBLv.rkhhL.cn
http://sSxOGgCb.rkhhL.cn
http://z58pvmx9.rkhhL.cn
http://6hFO6tdN.rkhhL.cn
http://f4bTrgAG.rkhhL.cn
http://OaFGOqpG.rkhhL.cn
http://RJH8Y8Da.rkhhL.cn
http://T2oaEB2y.rkhhL.cn
http://otZHRk3P.rkhhL.cn
http://yriiQtGQ.rkhhL.cn
http://UKdGWzes.rkhhL.cn
http://FLHVjGS5.rkhhL.cn
http://RK3wCJT8.rkhhL.cn
http://bB869B0O.rkhhL.cn
http://PzHRuFp5.rkhhL.cn
http://www.dtcms.com/a/382664.html

相关文章:

  • 利用python pandas库清洗病例处方清洗步骤
  • 数据库在并发访问时,不同隔离级别下脏读幻读问题
  • Python核心技术开发指南(065)——with语句
  • Python核心技术开发指南(064)——析构方法
  • 20250913-01: Langchain概念:Runnable可运行接口
  • 记一次谷歌语法获取路径 针对空白页面
  • Java GC:从GC Roots到分代设计的哲学
  • 一款4000℃高温材料设计方案及性能预测
  • 【leetcode】64. 最小路径和
  • 2.10组件间的通信
  • MinerU学习
  • 网络安全学习
  • 如何用 Rust 重写 SQLite 数据库(一):项目探索
  • Qwen3-80B-A3B混合注意力机制
  • OBS使用教程:OBS多路推流插件如何下载?如何安装使用?
  • 禁用 vscode 的终端的粘滞滚动
  • 人工智能通识与实践 - 人工智能概述
  • Symantec卸载
  • 第34章 AI在文娱与内容创作领域的应用
  • 学生信息管理系统(面向对象初步接触)
  • LangChain 中 Output Parsers 是什么?
  • Wolfspeed重组计划已确认
  • 【C++】继承机制深度解析:多继承与菱形继承
  • 如何用Maxscript在选择样条线顶点放置球体?
  • (LeetCode 面试经典 150 题) 190. 颠倒二进制位(位运算)
  • P1043题解
  • 如何用 Rust 重写 SQLite 数据库(二):项目探索
  • SQLI-labs[Part 2]
  • 如何安装 Prometheus 2.20.0 for Windows(amd64 版本详细步骤)​
  • 1004:字符三角形