第六场选拔 Problem E: Subarray GCD 水
来源:互联网 发布:数车的caxa编程 编辑:程序博客网 时间:2024/06/05 08:42
Problem E: Subarray GCD
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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,
int
d){
return
d==0?c:gcd(d,c%d);
}
int
main(){
int
T;
scanf
(
"%d"
,&T);
while
(T--){
int
n;
memset
(a,0,
sizeof
(a));
scanf
(
"%d"
,&n);
scanf
(
"%d"
,&a[0]);
int
logo=0;
int
asn=a[0];
for
(
int
i=1;i<n;i++)
{
scanf
(
"%d"
,&a[i]);
asn=gcd(asn,a[i]);
}
if
(asn==1)
printf
(
"%d\n"
,n);
else
printf
(
"-1\n"
);
}
return
0;}
阅读全文
0 0
- 第六场选拔 Problem E: Subarray GCD 水
- 第六场选拔 Problem G: 素数对 水
- 省赛选拔第六场
- 蓝桥选拔第一场(E)
- bc第六场Goffi and GCD
- 暑期个人赛--第六场--E
- Subarray GCD
- 第六届蓝桥杯校内选拔
- 2015多校联合第六场 hdu5373 The shortest problem 水
- 【河南省多校脸萌第六场 E】2358:LLM找对象
- Problem 1894 志愿者选拔
- Problem 1894 志愿者选拔
- 2013 - ECJTU 暑期训练赛第六场-problem-F
- 2013 - ECJTU 暑期训练赛第六场-problem-G
- 2013 - ECJTU 暑期训练赛第六场-problem-I
- 2013 - ECJTU 暑期训练赛第二场-problem-E
- 2013 - ECJTU 暑期训练赛第三场-problem-E
- 2013 - ECJTU 暑期训练赛第八场-problem-E
- vue中添加mp3音频文件
- dust学习
- 网站数据库连接_带端口
- Spring Web MVC4
- Adapter内的onItemClick监听器四个arg参数
- 第六场选拔 Problem E: Subarray GCD 水
- git工作流
- 命令汇总
- 修改Spring Boot的运行端口号
- mysql分区功能详细介绍,以及实例
- 将vim打造成IDE总结(一)
- #UVA1347#Tour(DP :双调欧几里得旅行商问题)
- Java数据类型
- leetcode 144. Binary Tree Preorder Traversal