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 of distinct positive integers.
Define a subarray to be a friend group if and only if there exists an integer such that , where denotes the remainder when is divided by .
Your friend Gregor wants to know the size of the largest friend group in .
Input
Each test contains multiple test cases. The first line contains the number of test cases .
Each test case begins with a line containing the integer , the size of the array .
The next line contains positive integers , representing the contents of the array . It is guaranteed that all the numbers in are distinct.
It is guaranteed that the sum of over all test cases is less than .
Output
Your output should consist of lines. Each line should consist of a single integer, the size of the largest friend group in .
Example
input
output
Note
In the first test case, the array is . The largest friend group is , since all those numbers are congruent to 0 modulo 2 , so .
In the second test case, the array is . The largest friend group is , since all those numbers are congruent to 2 modulo 3 , so .
In the third case, the largest friend group is . There are clearly many possible values of that work.
解题思路
st,gcd
即找到最长的一段区间 ,使 对 的余数相等,其中 ,即 ,即 ,将 记为 ,等价于找一段最长的区间且 ,注意如果差分数组有 项最长,但实际上最后求出来的共有 项满足条件,由于 可分段求,找任意区间区间 可用 表求解,同时求解最长时可从某个下标出发,先走长的,如果满足条件继续走直到不能走即 为止,此时走过的长度即为从当前下标开始的最长长度
- 时间复杂度:
代码
__EOF__

本文链接:https://www.cnblogs.com/zyyun/p/16167707.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!