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 ai,ai+1,,aj to be a friend group if and only if there exists an integer m2 such that aimodm=ai+1modm==ajmodm, where xmody 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(1t2104).
Each test case begins with a line containing the integer n(1n2105), the size of the array a.
The next line contains n positive integers a1,a2,,an(1ai1018), 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 2105.

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],使 al,al+1,,arm 的余数相等,其中 m>1,即 al%m=al+1%m==ar%m,即 |al+1al|%m=|al+2al+1|%m==|arar1|%m,将 |ai+1ai| 记为 di+1,等价于找一段最长的区间且 gcd>1,注意如果差分数组有 n1 项最长,但实际上最后求出来的共有 n 项满足条件,由于 gcd 可分段求,找任意区间区间 gcd 可用 ST 表求解,同时求解最长时可从某个下标出发,先走长的,如果满足条件继续走直到不能走即 gcd=1 为止,此时走过的长度即为从当前下标开始的最长长度

  • 时间复杂度:O(n×logn×log1018)

代码

// 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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16167707.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示