Integers Have Friends CodeForces - 1549D (线段树+尺取)

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 aa of distinct positive integers.

Define a subarray ai,ai+1,…,ajai,ai+1,…,aj to be a friend group if and only if there exists an integer m≥2m≥2 such that aimodm=ai+1modm=…=ajmodmaimodm=ai+1modm=…=ajmodm, where xmodyxmody denotes the remainder when xx is divided by yy.

Your friend Gregor wants to know the size of the largest friend group in aa.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2⋅1041≤t≤2⋅104).

Each test case begins with a line containing the integer nn (1≤n≤2⋅1051≤n≤2⋅105), the size of the array aa.

The next line contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤10181≤ai≤1018), representing the contents of the array aa. It is guaranteed that all the numbers in aa are distinct.

It is guaranteed that the sum of nn over all test cases is less than 2⋅1052⋅105.

Output

Your output should consist of tt lines. Each line should consist of a single integer, the size of the largest friend group in aa.

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

In the second test case, the array is [8,2,5,10][8,2,5,10]. The largest friend group is [8,2,5][8,2,5], since all those numbers are congruent to 22 modulo 33, so m=3m=3.

In the third case, the largest friend group is [1000,2000][1000,2000]. There are clearly many possible values of mmthat work.

思路:

a%m=b%m 可以得出(a-b)是m的倍数 ,显然m不能是1,又借助

如果a[i]与a[j]的差值是m的倍数, a[j]与a[k]的差值也是m的倍数, 则a[i]与a[k]的差值也一定为m的倍数.

我们让b[i]=a[i]-a[i-1];

于是只要一个区间【l,r】内的所有b[i]的最大公因数不等于1,说明必存在一个m让区间满足a[i]%m相等。

于是本题可以转化成:求gcd(b[l , r])!=1 的最大区间(查询可以用线段树)

可以枚举所有 r 显然这会超时,因此再想 假如说: r的最大区间为l~r,枚举r+1时l有必要从2开始吗? l之前的的数到r肯定最大公因数是一啊,多一个r+1那肯定还是1啊,所以继续从l开始,这种思想那肯定是尺取了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long m,n,a[1001010],b[1010010],tree[1010013];
long long abs1(long long x)
{
	if(x<0) x=-x;
	return x;
}
long long gcd1(long long  a, long long b)
{
	return b ? gcd1(b, a%b) : a;
}

void build(long long node,long long l,long long r)
{
	long long mid;
	if(l==r)
	{
		tree[node]=b[l];
		return;
	}
	mid=(l+r)/2;
	build(node*2,l,mid);
	build(node*2+1,mid+1,r);
	tree[node]=gcd1(tree[node*2],tree[node*2+1]);
}

long long query(long long node,long long l,long long r,long long L,long long R)
{
	long long x1,x2,mid;

	if(l>=L&&r<=R)return tree[node];

	if(l>R||r<L) return 0;

	mid=(r+l)/2;
	x1=query(node*2,l,mid,L,R);
	x2=query(node*2+1,mid+1,r,L,R);
	if(x1&&x2) return gcd1(x1,x2);
	if(x1&&!x2) return x1;/*返回0 说明这个区间不在查询范围之内,就不去求gcd了*/
	if(!x1&&x2) return x2;
}
int main()
{
	long long i,j,l,r,w,f,max1;
	scanf("%lld",&w);
	while(w--)
	{
		scanf("%lld",&n);
		f=0;
		for(i=1; i<=n; i++)
		{
			scanf("%lld",&a[i]);
			if(i>1)
			{
				b[i]=abs1(a[i]-a[i-1]);
				if(b[i]!=1)
					f=1;
			}
		}
		if(!f)/*如果都是相邻差都是1,那肯定任意相邻两个都不符合题意了只能自己一个*/
		{
			printf("1\n");
			continue;
		}
		build(1,2,n);/*用线段树维护区间gcd*/
		
		l=2;
		max1=0;
		for(r=2; r<=n; r++)
		{
			if(b[r]==1)/*b[r]是1,l~r-1 到 r的gcd肯定是1直接过*/
			{
				l=r+1;
				continue;
			}
			while(l<=r&&query(1,2,n,l,r)==1) l++;/*尺取*/			
			
			max1=max(r-l+1,max1);
		}
		
		printf("%lld\n",max1+1);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值