Description
Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that
GCD of all integers in that subarray is 1.
Formally,
For a subarray Ai,Ai+1...Aj where 1 ≤ i < j ≤ N to be valid: GCD(Ai,Ai+1...Aj) should be 1. You have to print the size of the largest valid subarray.
If no valid subarray exists, output -1.
Note:A single element is not considered as a subarray according to the definition of this problem.
Input
First line contains T, the number of testcases. Each testcase consists of N in one line followed by Nintegers in the next line.
Constraints
1 ≤ T ≤ 10
2 ≤ N ≤ 105
1 ≤ Ai ≤ 105
Output
For each testcase, print the required answer in one line.
Sample Input
2
2
7 2
3
2 2 4
Sample Output
2
Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that
GCD of all integers in that subarray is 1.
Formally,
For a subarray Ai,Ai+1...Aj where 1 ≤ i < j ≤ N to be valid: GCD(Ai,Ai+1...Aj) should be 1. You have to print the size of the largest valid subarray.
If no valid subarray exists, output -1.
Note:A single element is not considered as a subarray according to the definition of this problem.
Input
First line contains T, the number of testcases. Each testcase consists of N in one line followed by Nintegers in the next line.
Constraints
1 ≤ T ≤ 10
2 ≤ N ≤ 105
1 ≤ Ai ≤ 105
Output
For each testcase, print the required answer in one line.
Sample Input
2
2
7 2
3
2 2 4
Sample Output
2
-1
题目大意:给你一个序列,问你 他们的最大公约数为1的最大长度是多少
解题思路:如果有两个数的最大公约数为1,则这个序列所有的数的公约数肯定为1.
代码:
#include <iostream>
#include <cstdio>
using namespace std;
int gcd(int a,int b)
{
while(b)
{
int temp=a%b;
a=b;
b=temp;
}
return a;
}
int main()
{
int t,a,b,n;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&a);
for(int i=1;i<n;i++)
{
scanf("%d",&b);
a=gcd(a,b);
}
if(a==1)
printf("%d\n",n);
else
printf("-1\n");
}
return 0;
}