#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10,mod=1e9+7;
#define PII pair<int,int>
// #define x first
// #define y second
#define PB push_back
bool vis[N];
ll ST[N][100];
ll b[N],a[N];
int n;
void ST_init()
{
for(int i=1;i<=n;++i)
ST[i][0]=a[i];
for(int j=1;j<32;++j)
{
for(int i=1;i<=n;++i)
{
if(i+(1<<j)-1>n) break;
if((!ST[i+(1<<(j-1))][j-1])){
if(!ST[i][j-1]){
ST[i][j]=0;
}
else{
ST[i][j]=1;
}
continue;
}
ST[i][j]=__gcd(ST[i][j-1],ST[i+(1<<(j-1))][j-1]);
}
}
}
ll ST_query(int s,int e)
{
int k=(int)((log(e-s+1.0)/log(2.0)));
return __gcd(ST[s][k],ST[e-(1<<k)+1][k]);
}
void solve()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>b[i];
for(int i=1;i<n;i++){
a[i]=abs(b[i+1]-b[i]);
}
if(n==1){
cout<<1<<endl;return;
}
if(n==2){
if(b[1]==1||b[2]==1){
cout<<1<<endl;
}
else cout<<2<<endl;
return;
}
ST_init();
// if(n==200000){
// cout<<ST_query(1,n-1)<<endl;
// }
int ans=1;
for(int i=1;i<n;i++){
// cout<<ST_query(i,n-1)<<endl;
int l=i,r=n-1;
int mid;
while(l<=r){
mid=(l+r)>>1;
//cout<<ST_query(i,mid)<<endl;
if(ST_query(i,mid)>1||ST_query(i,mid)==0){
l=mid+1;
}
else{
r=mid-1;
}
}
if(ST_query(i,r)>1||(ST_query(i,mid)==0))
ans=max(r-i+2,ans);
}
cout<<ans<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("a.txt", "r", stdin);
freopen("aout.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while (t--)
solve();
}
Codeforces Round #736 (Div. 2) D ST表维护区间gcd,二分
最新推荐文章于 2022-11-10 16:32:57 发布