LGTB 与序列
LGTB 得到了一个序列,他想在这个序列中选择一个最长的连续子序列,使得这个子序列的最大公约数等于1。请告诉他他能得到的最大长度,如果没有这样的序列,输出-1
输入
输入第一行包含一个整数n 代表序列大小
接下来一行,包含n 个整数a1, a2, …, an,代表序列
对于50% 的数据,1 n 1000
对于100% 的数据,1 n 105 1 ai 109
输出
输出包含一个整数l,代表最长的连续子序列,如果无解请输出-1
样例
样例输入 样例输出
2 2
7 2
样例输入 样例输出
3 -1
2 2 4
er.ZZ水题直接GCD
附代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#define clr(a,x) memset(x,a,sizeof(x))
#define ll long long
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
int n;
template <class T> inline void read(T &xx)
{
xx = 0;
T flag = 1;
char ch = (char)getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = (char)getchar();
}
while(ch>='0' && ch<='9')
{
xx = (xx<<1) + (xx<<3) + ch - '0';
ch = (char)getchar();
}
xx *= flag;
}
int gcd(int a, int b)
{
return !b ? a : gcd(b, a%b);
}
void init()
{
read(n);
}
void work()
{
int a,b = 0;
for(int i=1;i<=n;i++)
{
read(a);
b = gcd(b,a);
}
if(b == 1) printf("%d",n);
else printf("-1");
}
int main()
{
freopen("seq.in","r",stdin);
freopen("seq.out","w",stdout);
init();
work();
return 0;
}