第六场选拔 Problem E: Subarray GCD 水

来源:互联网 发布:数车的caxa编程 编辑:程序博客网 时间:2024/06/05 08:42

Problem E: Subarray GCD

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 68  Solved: 34
[Submit][Status][Web Board]

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
-1


题意是求最大的一个序列,这个序列的所有数最大公约数为1,仔细想想,给定序列只要有一个子序列最大公约数为1,那么整个必定为1,如果不存在,那么这个序列的最大公约数不为1,所以把序列所有数取公约就好了。



#include<cstdio>
#include<cstring>
int a[100005];
int gcd(int c,intd){
    returnd==0?c:gcd(d,c%d);
}
int main(){
    intT;
    scanf("%d",&T);
    while(T--){
        intn;
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        scanf("%d",&a[0]);
        intlogo=0;
        intasn=a[0];
        for(inti=1;i<n;i++)
        {
            scanf("%d",&a[i]);
            asn=gcd(asn,a[i]);
 
        }
        if(asn==1)
            printf("%d\n",n);
        elseprintf("-1\n");
 
    }
return0;}