B. Integers Have Friends

4 篇文章 0 订阅

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.

题意:给你n个正数,找到最长一段区间l~r,使这里面的所有数满足

a[l]%m==a[i+1]%m==...==a[r]%m  (m是>=2的一个数)

思路:因为每个数的余数不好确定,所以我们利用差分数组来消除a[i]%m==k中k的计算

然后差分数组b中,满足题意的区间里的所有数都是m的倍数,m就是这个区间的公约数

因为m>=2,所以我们只需要找到gcd>=2的区间的最大长度就可以了

解决区间最值问题用RMQ中的st表来解决

(没了解过st表的可以移步我的另一篇来看:RMQ(区间最值问题)_Demoo.的博客-CSDN博客

/*

 .----------------.  .----------------.  .----------------.  .----------------. 
| .--------------. || .--------------. || .--------------. || .--------------. |
| |  ________    | || |  _________   | || | ____    ____ | || |     ____     | |
| | |_   ___ `.  | || | |_   ___  |  | || ||_   \  /   _|| || |   .'    `.   | |
| |   | |   `. \ | || |   | |_  \_|  | || |  |   \/   |  | || |  /  .--.  \  | |
| |   | |    | | | || |   |  _|  _   | || |  | |\  /| |  | || |  | |    | |  | |
| |  _| |___.' / | || |  _| |___/ |  | || | _| |_\/_| |_ | || |  \  `--'  /  | |
| | |________.'  | || | |_________|  | || ||_____||_____|| || |   `.____.'   | |
| |              | || |              | || |              | || |              | |
| '--------------' || '--------------' || '--------------' || '--------------' |
 '----------------'  '----------------'  '----------------'  '----------------'

*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b){
	return b>0 ? gcd(b,a%b):a;
}
/*
int dx[8]={-2,-2,-1,1,2,2,-1,1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
*/
//int e[N],ne[N],h[N],idx,w[N];
/*void add(int a,int b,int c){
	e[idx]=b;
	w[idx]=c;
	ne[idx]=h[a];
	h[a]=idx++;
}
*/
//满足条件的区间的每个数摸上m都是同一个值x,那么用差分去掉余数x的影响,那么我们找到的差分数组的满足的区间就是m的倍数
//m>=2,那么我们的差分数组满足的所有数的gcd>=2;
//就转换为找差分数组的gcd>=2最大区间长度
const int N=2e5+10;
int n;
int a[N],b[N];
int f[N][20];
void st(){
	for(int j=0;j<=19;j++){
		for(int i=1;i+(1<<j)-1<=n;i++){
			if(j==0)f[i][j]=b[i];
			else f[i][j]=gcd(f[i][j-1],f[i+(1<<j-1)][j-1]);
		}
	}
}
int query(int l,int r){
	int k=log2(r-l+1);
	return gcd(f[l][k],f[r-(1<<k)+1][k]);
}
void sove(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	if(n==1){
		cout<<1<<endl;
		return ;
	}
	for(int i=1;i<n;i++)b[i]=abs(a[i+1]-a[i]);//处理差分,注意gcd都是正数,而且取模之后有的数大有的数小,所以差分数组我们取绝对值
	n--;
	int ans=0;//找差分数组gcd大于1的区间的长度的最大值
	st();
	for(int i=1;i<=n;i++){
		int l=i,r=n;
		while(l<r){
			int mid=l+r+1>>1;
			if(query(i,mid)==1)r=mid-1;
			else l=mid;
		}
		if(query(i,l)!=1)ans=max(ans,l-i+1);
	}
	cout<<ans+1<<endl;
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie() ,cout.tie() ;
	int t=1;
	cin>>t;
	while(t--){
		sove();
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值