Problem E: Subarray GCD

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 44  Solved: 27
[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
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
using namespace std;
int t, n,k,ans;
int gcd(int x,int y)
{
return y==0 ? x : gcd(y,x%y);
} int main()
{
cin>>t;
while(t--){
cin>>n; cin>>ans;
for (int i=2;i<=n;i++)//范围!
{
cin>>k;
ans=gcd(ans,k);//递归!
}
if (ans==1) cout<<n<<endl; else cout<<"-1"<<endl;
}
return 0;
}

  

ZCMU Problem E: Subarray GCD(n个数的最大公约数)的更多相关文章

  1. hdu 4630 查询[L,R]区间内任意两个数的最大公约数

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. Java数据结构与算法之---求两个数的最大公约数(欧几里得算法)

    一个简单的小算法来获取两个数的最大公约数, public class Test { public static void main(String[] args) { long result = gcd ...

  3. [华为机试练习题]55.最大公约数 &amp; 多个数的最大公约数

    题目 描写叙述: 输入2个数字,最后输出2个数字的最大公约数 题目类别: 位运算 难度: 0基础 执行时间限制: 无限制 内存限制: 无限制 阶段: 入职前练习 输入: 2个整数 输出: 输出数字1和 ...

  4. C++扬帆远航——14(求两个数的最大公约数)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:gongyueshu.cpp * 作者:常轩 * 微信公众号:W ...

  5. C语言辗转相除法求2个数的最小公约数

    辗转相除法最大的用途就是用来求两个数的最大公约数. 用(a,b)来表示a和b的最大公约数. 有定理: 已知a,b,c为正整数,若a除以b余c,则(a,b)=(b,c). (证明过程请参考其它资料) 例 ...

  6. 求N个数的最大公约数和最小公倍数(转)

    除了分解质因数,还有另一种适用于求几个较小数的最大公约数.最小公倍数的方法 下面是数学证明及算法实现 令[a1,a2,..,an] 表示a1,a2,..,an的最小公倍数,(a1,a2,..,an)表 ...

  7. C++中用辗转相除法求两个数的最大公约数和最小公倍数

    两个数的最大公约数:不能大于两个数中的最小值,算法口诀:小的给大的,余数给小的,整除返回小的,即最大公约数,(res=max%min)==0?  max=min,min=res return min; ...

  8. c语言实践:求两个数的最大公约数

    我的思路是这样的:比如12和16这两个数.先理解一下概念,什么叫最大公约数.就是12有很多个因数,16也有很多个因数,这两堆因数中有一些重合的因数,在这些重合的因数中找到那个最大的.那么最大公约数一定 ...

  9. python 函数求两个数的最大公约数和最小公倍数

    1. 求最小公倍数的算法: 最小公倍数  =  两个整数的乘积 /  最大公约数 所以我们首先要求出两个整数的最大公约数, 求两个数的最大公约数思路如下: 2. 求最大公约数算法: 1. 整数A对整数 ...

随机推荐

  1. servletContext的定义

  2. 【bzoj1797】[Ahoi2009]Mincut 最小割 网络流最小割+Tarjan

    题目描述 给定一张图,对于每一条边询问:(1)是否存在割断该边的s-t最小割 (2)是否所有s-t最小割都割断该边 输入 第一行有4个正整数,依次为N,M,s和t.第2行到第(M+1)行每行3个正 整 ...

  3. WebSocket对象的创建及其与WebSocket服务器的连接(5)

    WebSocket接口的使用非常简单,要连接通信端点,只需要创建一个新的WebSocket实例,并提供希望连接URL. //ws://和wss://前缀分别表示WebSocket连接和安全的WebSo ...

  4. [洛谷P4940]Portal2

    题目大意:维护两个栈,几个操作: $PUSH\;x\;num:$把$num$压入栈$x$ $POP\;x:$弹出栈$x$栈顶元素 $ADD\;x:$取出两个栈栈顶,把相加值压入栈$x$ $SUB\;x ...

  5. Python之利用reduce函数求序列的最值及排序

    在一般将Python的reduce函数的例子中,通常都是拿列表求和来作为例子.那么,是否还有其他例子呢?   本次分享将讲述如何利用Python中的reduce函数对序列求最值以及排序.   我们用r ...

  6. [Leetcode] remove element 删除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  7. npm错误总结

    You cannot publish over the previously published version 1.0.1." : xxx 发布时一定要修改package.json的版本号 ...

  8. Dilworth定理证明

    命题:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. (离散数学结构第六版课本P245:把一个偏序集划分成具有全序的子集所需要的最少子集个数与元素在偏序下都是不可比的最大集合的基数之间有 ...

  9. codeforces 1060 C

    https://codeforces.com/contest/1060/problem/C 题意:给你一个长度为n的数列a和长度为m的数列b,定义c(i,j)=ai*bj,得到c矩阵,给定值x,求c矩 ...

  10. codeforces 799C Fountains

    C. Fountains time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...