浙江中医药大学暑期训练测试赛八F

1571: Subarray GCD

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 90   Solved: 48
[ 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
7 2
3
2 2 4

Sample Output

2
-1

AC代码:
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
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;
        scanf("%d",&t);
        while(t--)
        {
                int n;
                scanf("%d",&n);
                int x;
                scanf("%d",&x);
                for(int i=1;i<n;i++)
                {
                        int temp;
                        scanf("%d",&temp);
                        x=gcd(x,temp);
                }
                if(x==1)
                        printf("%d\n",n);
                else
                        printf("-1\n");
        }
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值