题意:
给你一个序列
a
a
a,求一个最长的子序列
[
l
,
r
]
[l,r]
[l,r]满足
a
i
m
o
d
m
=
a
i
+
1
m
o
d
m
=
.
.
.
=
a
r
m
o
d
m
a_i\bmod m=a_{i+1}\bmod m=...=a_r\bmod m
aimodm=ai+1modm=...=armodm,其中
m
≥
2
m\ge2
m≥2。
n
≤
2
e
5
,
a
i
≤
1
e
18
n\le2e5,a_i\le1e18
n≤2e5,ai≤1e18
思路:
之前牛客做过一个类似的题,但是哪个题是要你找一个最小模数使得每个数模上他之后各不相同,这个题大同小异。
首先如果他们取模相等的话,那么每个相邻的两个之差的因数都可以作为
m
m
m,问题转换成了求
c
i
=
a
i
+
1
−
a
i
c_i=a_{i+1}-a_i
ci=ai+1−ai数组中最长的
g
c
d
gcd
gcd不为
1
1
1的序列。
这个显然可以用尺取来解决,数越少
g
c
d
gcd
gcd可能越大,满足单调性。用
S
T
ST
ST表预处理一下即可。
// Problem: D. Integers Have Friends
// Contest: Codeforces - Codeforces Round #736 (Div. 2)
// URL: https://codeforces.com/contest/1549/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=400010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n;
LL a[N],c[N];
LL f[N][25];
int len[N];
void init(int n)
{
for(int i=1;i<=n;i++) len[i]=log2(i);
for(int i=1;i<=n;i++) f[i][0]=c[i];
int t=len[n]+1;
for(int j=1;j<t;j++)
for(int i=1;i<=n-(1<<j)+1;i++)
f[i][j]=__gcd(f[i][j-1],f[i+(1ll<<(j-1))][j-1]);
}
LL query(int l,int r) {
int t=len[r-l+1];
return __gcd(f[l][t],f[r-(1<<t)+1][t]);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int _; scanf("%d",&_);
while(_--) {
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<=n-1;i++) c[i]=abs(a[i+1]-a[i]);
init(n-1);
int ans=1;
for(int i=1,j=1;i<=n-1;i++) {
while(j<=i&&query(j,i)==1) j++;
ans=max(ans,i-j+2);
}
printf("%d\n",ans);
}
return 0;
}
/*
*/