British mathematician John Littlewood once said about Indian mathematician Srinivasa Ramanujan that “every positive integer was one of his personal friends.”
It turns out that positive integers can also be friends with each other! You are given an array a of distinct positive integers.
Define a subarray a i , a i + 1 , … , a j a_i,a_{i+1},…,a_j ai,ai+1,…,aj to be a friend group if and only if there exists an integer m ≥ 2 m≥2 m≥2 such that a i mod m = a i + 1 mod m = … = a j mod m a_i ~\text{mod}~m=a_{i+1}~\text{mod}~m=…=a_j~\text{mod}~m ai mod m=ai+1 mod m=…=aj mod m, where x mod y x~\text{mod}~y x mod y denotes the remainder when x x x is divided by y y y.
Your friend Gregor wants to know the size of the largest friend group in a.
Input
Each test contains multiple test cases. The first line contains the number of test cases t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 ) t (1≤t≤2⋅10^4) t(1≤t≤2⋅104).
Each test case begins with a line containing the integer n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n (1≤n≤2⋅10^5) n(1≤n≤2⋅105), the size of the array a.
The next line contains n positive integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 1 0 18 ) a_1,a_2,…,a_n (1≤a_i≤10^{18}) a1,a2,…,an(1≤ai≤1018), representing the contents of the array a. It is guaranteed that all the numbers in a are distinct.
It is guaranteed that the sum of n n n over all test cases is less than 2 ⋅ 1 0 5 2⋅10^5 2⋅105.
Output
Your output should consist of t lines. Each line should consist of a single integer, the size of the largest friend group in a.
Example
input
4
5
1 5 2 4 6
4
8 2 5 10
2
1000 2000
8
465 55 3 54 234 12 45 78
output
3
3
2
6
Note
In the first test case, the array is [ 1 , 5 , 2 , 4 , 6 ] [1,5,2,4,6] [1,5,2,4,6]. The largest friend group is [ 2 , 4 , 6 ] [2,4,6] [2,4,6], since all those numbers are congruent to 0 0 0 modulo 2 2 2, so m = 2 m=2 m=2.
In the second test case, the array is [ 8 , 2 , 5 , 10 ] [8,2,5,10] [8,2,5,10]. The largest friend group is [ 8 , 2 , 5 ] [8,2,5] [8,2,5], since all those numbers are congruent to 2 2 2 modulo 3 3 3, so m = 3 m=3 m=3.
In the third case, the largest friend group is [ 1000 , 2000 ] [1000,2000] [1000,2000]. There are clearly many possible values of m m m that work.
思路
显然题意可以转换为,求一个最大的 l e n len len ,使得差分数组存在一个长度为 l e n len len 的连续子序列,该子序列的 g c d > 1 gcd>1 gcd>1。
设两个指针 l , r l,r l,r,它们确定一个区间 [ l , r ] [l,r] [l,r] 。若该区间 g c d > 1 gcd>1 gcd>1,那么 r + + r++ r++ ;否则 l + + l++ l++。这样 O ( n ) O(n) O(n) 扫描一遍,不断更新最值就可以了。
查询 [ l , r ] [l,r] [l,r] 的 g c d gcd gcd 可以用线段树或者 S T ST ST 表。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int T,n,a[N],b[N];
struct segment{ int l,r,gcd; }tree[N<<2];
void build(int p,int l,int r){
tree[p].l=l,tree[p].r=r;
if(l==r){ tree[p].gcd=b[l]; return; }
int mid=(l+r)>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
tree[p].gcd=__gcd(tree[p<<1].gcd,tree[p<<1|1].gcd);
}
int ask(int p,int l,int r){
if(l<=tree[p].l&&r>=tree[p].r) return tree[p].gcd;
int mid=(tree[p].l+tree[p].r)>>1;
int ret=0;
if(l<=mid) ret=__gcd(ret,ask(p<<1,l,r));
if(mid+1<=r) ret=__gcd(ret,ask(p<<1|1,l,r));
return ret;
}
void solve(){
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=1;i<n;i++) b[i]=abs(a[i]-a[i-1]);
if(n==1) return (void)(cout<<"1\n");
build(1,1,n-1);
int mx=0;
for(int l=1,r=1;r<n;r++){
if(ask(1,l,r)>1) mx=max(mx,r-l+1);
else while(ask(1,l,r)==1) l++;
}
cout<<mx+1<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
for(cin>>T;T;T--) solve();
}
方法二
这样暴力居然也能过。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int T,n,a[N],b[N];
void solve(){
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=1;i<n;i++) b[i]=abs(a[i]-a[i-1]);
int gcd=0,len=0,mx=0;
for(int i=1;i<n;i++){
gcd=__gcd(gcd,b[i]);
if(gcd==1){
gcd=len=0;
for(int j=i;j>=1;j--){
int t=__gcd(gcd,b[j]);
if(t>1) gcd=t,len++,mx=max(len,mx);
else break;
}
}else len++,mx=max(len,mx);
}
cout<<mx+1<<"\n";
}
signed main(){
ios::sync_with_stdio(false);
for(cin>>T;T;T--) solve();
}