浙江中医药大学暑期训练测试赛八F
2017-07-31 15:13
155 查看
1571: Subarray GCD
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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 thatGCD 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 ≤ 102 ≤ N ≤ 105
1 ≤ Ai ≤ 105
Output
For each testcase, print the required answer in one line.Sample Input
22
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)
4000
printf("%d\n",n);
else
printf("-1\n");
}
return 0;
}
相关文章推荐
- 浙江中医药大学暑期训练测试赛八B
- 浙江中医药大学暑期训练测试赛八H
- 浙江中医药大学暑期训练测试赛十 英雄无敌3(2) 数学公式
- 浙江中医药大学暑期训练测试赛八I
- 浙江中医药大学暑期训练测试赛八G
- 浙江中医药大学暑期训练测试赛十
- 浙江中医药大学暑期训练测试赛八C
- 浙江中医药大学暑期训练测试赛八E
- 浙江中医药大学暑期训练测试赛八A
- 暑期测试训练3
- CSU-ACM2017暑期训练3-递推与递归 Erratic Expansion
- 4000 windows-caffe 训练和测试自己的数据集
- 3000门徒内部训练绝密视频(泄密版)第6课:精通Spark集群搭建与测试
- [置顶] Faster rcnn 安装、训练、测试、调试
- caffe手写数字mnist训练测试使用教程
- CSU-ACM2017暑期训练10-并查集&&HASH A-Virtual Friends HDU - 3172
- CSU-ACM2017暑期训练12-KMP E - 循环
- Caffe学习系列(12):训练和测试自己的图片--linux平台
- caffe--python版利用训练好模型进行测试
- CSU-ACM2017暑期训练16-树状数组 A - Ultra-QuickSort POJ - 2299