D. Integers Have Friends

题目链接

D. Integers Have Friends

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}, \ldots, a_{j}\) to be a friend group if and only if there exists an integer \(m \geq 2\) such that \(a_{i} \bmod m=a_{i+1} \bmod m=\ldots=a_{j} \bmod m\), where \(x \bmod y\) denotes the remainder when \(x\) is divided by \(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\left(1 \leq t \leq 2 \cdot 10^{4}\right)\).
Each test case begins with a line containing the integer \(n\left(1 \leq n \leq 2 \cdot 10^{5}\right)\), the size of the array \(a\).
The next line contains \(n\) positive integers \(a_{1}, a_{2}, \ldots, a_{n}\left(1 \leq a_{i} \leq 10^{18}\right)\), 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\) over all test cases is less than \(2 \cdot 10^{5}\).

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]\). The largest friend group is \([2,4,6]\), since all those numbers are congruent to 0 modulo 2 , so \(m=2\).

In the second test case, the array is \([8,2,5,10]\). The largest friend group is \([8,2,5]\), since all those numbers are congruent to 2 modulo 3 , so \(m=3\).
In the third case, the largest friend group is \([1000,2000]\). There are clearly many possible values of \(m\) that work.

解题思路

st,gcd

即找到最长的一段区间 \([l,r]\),使 \(a_l,a_{l+1},\dots,a_r\)\(m\) 的余数相等,其中 \(m>1\),即 \(a_l\%m=a_{l+1}\%m=\dots=a_r\%m\),即 \(|a_{l+1}-a_l|\%m=|a_{l+2}-a_{l+1}|\%m=\dots=|a_{r}-a_{r-1}|\%m\),将 \(|a_{i+1}-a_i|\) 记为 \(d_{i+1}\),等价于找一段最长的区间且 \(gcd>1\),注意如果差分数组有 \(n-1\) 项最长,但实际上最后求出来的共有 \(n\) 项满足条件,由于 \(gcd\) 可分段求,找任意区间区间 \(gcd\) 可用 \(ST\) 表求解,同时求解最长时可从某个下标出发,先走长的,如果满足条件继续走直到不能走即 \(gcd=1\) 为止,此时走过的长度即为从当前下标开始的最长长度

  • 时间复杂度:\(O(n\times logn\times log10^{18})\)

代码

// Problem: B. Integers Have Friends
// Contest: Codeforces - Codeforces Round #736 (Div. 1)
// URL: https://codeforces.com/contest/1548/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=2e5+5;
int T,n,res;
LL a[N],d[N],st[N][25];

int main()
{
    for(read(T);T;T--)
    {
    	read(n);
    	for(int i=1;i<=n;i++)read(a[i]),d[i]=abs(a[i]-a[i-1]),st[i][0]=d[i];
    	res=1;
    	int t=log(n)/log(2)+1;
    	for(int i=1;i<=t;i++)
    		for(int j=1;j+(1<<i-1)-1<n&&j+(1<<(i-1))<=n;j++)
    			st[j][i]=__gcd(st[j][i-1],st[j+(1<<(i-1))][i-1]);
    	for(int i=2;i<=n;i++)
    	{
    		int tt=i;
    		LL now=0;
    		for(int j=t;j>=0;j--)
    			if(tt+(1<<j)-1<=n)
    			{
    				LL gd=__gcd(now,st[tt][j]);
    				if(gd>1)tt+=1<<j,now=gd;
    			}
    		res=max(res,tt-i+1);
    	}
    	printf("%d\n",res);
    }
    return 0;
}
posted @ 2022-04-19 22:19  zyy2001  阅读(47)  评论(0编辑  收藏  举报