A14435 | Integers Have Friends
时间限制1s
内存限制256MB
通过 / 提交0/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 $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 \ge 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$ .
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 \ge 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$ .
输入格式
Each test contains multiple test cases. The first line contains the number of test cases $t$ ( $1 \le t \le 2\cdot 10^4$ ).
Each test case begins with a line containing the integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ), the size of the array $a$ .
The next line contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le {10}^{18}$ ), 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$ .
Each test case begins with a line containing the integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ), the size of the array $a$ .
The next line contains $n$ positive integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_i \le {10}^{18}$ ), 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$ .
输出格式
Your output should consist of $t$ lines. Each line should consist of a single integer, the size of the largest friend group in $a$ .
输入输出样例
输入 #1
4 5 1 5 2 4 6 4 8 2 5 10 2 1000 2000 8 465 55 3 54 234 12 45 78
输出 #1
3 3 2 6
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted