B. Make Almost Equal With Mod

time limit per test

1 second

memory limit per test

256 megabytes

xi - Solar Storm

You are given an array a1,a2,...,an of distinct positive integers. You have to do the following operation exactly once:

  • choose a positive integer k;
  • for each i from 1 to n, replace ai with ai mod k†.

Find a value of k such that 1≤k≤1018 and the array a1,a2,...,an contains exactly 2 distinct values at the end of the operation. It can be shown that, under the constraints of the problem, at least one such k always exists. If there are multiple solutions, you can print any of them.

† a mod b denotes the remainder after dividing a by b. For example:

  • 7 mod 3=1 since 7=3⋅2+1
  • 15 mod 4=3 since 15=4⋅3+3
  • 21 mod 1=0 since 21=21⋅1+0

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤500). The description of the test cases follows.

The first line of each test case contains a single integer n (2≤n≤100) --- the length of the array a.

The second line of each test case contains n integers a1,a2,...,an (1≤ai≤1017) --- the initial state of the array. It is guaranteed that all the ai are distinct.

Note that there are no constraints on the sum of n over all test cases.

Output

For each test case, output a single integer: a value of k (1≤k≤1018) such that the array a1,a2,...,an contains exactly 2 distinct values at the end of the operation.

Example

Input

Copy

复制代码

5

4

8 15 22 30

5

60 90 98 120 308

6

328 769 541 986 215 734

5

1000 2000 7000 11000 16000

2

2 1

Output

Copy

复制代码
7
30
3
5000
1000000000000000000

Note

In the first test case, you can choose k=7. The array becomes 8 mod 7,15 mod 7,22 mod 7,30 mod 7=1,1,1,2, which contains exactly 2 distinct values ({1,2}).

In the second test case, you can choose k=30. The array becomes 0,0,8,0,8, which contains exactly 2 distinct values ({0,8}). Note that choosing k=10 would also be a valid solution.

In the last test case, you can choose k=1018. The array becomes 2,1, which contains exactly 2 distinct values ({1,2}). Note that choosing k=1018+1 would not be valid, because 1≤k≤1018 must be true.

解题说明:此题是一道数学题,选择一个正整数k,将数组每个元素变为a(i)%k ,求解k设为什么值可以使得原数组有且仅有两种值。找规律能发现只需要找到所有元素相对于第一个元素的偏移量的最大公约数,然后乘以2就是需要的值。

cpp 复制代码
#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main() 
{
    long long int t, n; 
    cin >> t;
    while (t-- && cin >> n) 
    {
        long long int a[n], k = 0; 
        for (int i = 0; i < n; i++)
        {
            cin >> a[i];
            k = __gcd(k, abs(a[i] - a[0]));
        }
        cout << k + k << '\n';
    }
    return 0;
}
相关推荐
徐小夕1 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
akunkuntaimei1 小时前
2026年高考数学各省真题及答案(完整版)
算法·高考
Hello:CodeWorld2 小时前
C 风格变参 vs C++ 变参模板:核心区别与选型指南
c语言·c++·算法
8Qi83 小时前
LeetCode 516:最长回文子序列
算法·leetcode·职场和发展·动态规划
youngerwang4 小时前
【从搬运工到协处理器:网卡芯片架构、算法、验证与边缘演进深度剖析】
网络·算法·架构·芯片
想要成为糕糕手4 小时前
前端必修课:JavaScript 数组与数据结构底层逻辑全解析
javascript·数据结构·面试
KaMeidebaby4 小时前
卡梅德生物技术快报|纯化重组蛋白实操详解
人工智能·python·tcp/ip·算法·机器学习
手写码匠5 小时前
从零实现 Prompt 工程引擎:结构化提示、自动优化与多轮自省体系
人工智能·深度学习·算法·aigc
无限码力6 小时前
阿里算法岗 0530笔试真题 - 多约束条件下的元素匹配统计
算法·阿里笔试真题·阿里机试真题·阿里算法岗笔试
lqqjuly6 小时前
MLA — 多头潜在注意力深度解析
深度学习·神经网络·算法