题目链接
题目描述
Fried-chicken hates origami problems! He always fails at solving origami problems that others can solve easily (for example, CF1966E and HDU6822). However, today Fried-chicken is the problem setter! It’s his turn to give contestants a very difficult origami problem!
Given an array a\textstyle aa of length n\textstyle nn, you can perform the following operations any number of times (possibly zero):
Choose an index p\textstyle pp, and then perform one of the following two operations:
-
For all i\textstyle ii such that ai≤ap\textstyle a_i \leq a_pai≤ap, let ai←ai+2(ap−ai)\textstyle a_i \gets a_i + 2(a_p - a_i)ai←ai+2(ap−ai).
-
For all i\textstyle ii such that ai≥ap\textstyle a_i \geq a_pai≥ap, let ai←ai−2(ai−ap)\textstyle a_i \gets a_i - 2(a_i - a_p)ai←ai−2(ai−ap).
For example, if the original array is [2,4,5,3]\textstyle [2,4,5,3][2,4,5,3] and you choose p=2\textstyle p=2p=2 and perform operation 1, the array will become [6,4,5,5]\textstyle [6,4,5,5][6,4,5,5].
Now, you want to minimize the range of the array through these operations. Recall that the range of an array is the difference between the maximum and minimum elements of the array.
输入描述:
Each test contains multiple test cases. The first line contains the number of test cases t\textstyle tt (1≤t≤5×105\textstyle 1 \leq t \leq 5 \times 10^51≤t≤5×105). The description of the test cases follows.
The first line of each test case contains an integer n\textstyle nn (1≤n≤105\textstyle 1 \leq n \leq 10^51≤n≤105), representing the length of the array.
The second line contains n\textstyle nn integers a1,a2,…,an\textstyle a_1, a_2, \ldots, a_na1,a2,…,an (0≤ai≤1016\textstyle 0 \leq a_i \leq 10^{16}0≤ai≤1016), describing the elements of the array a\textstyle aa in the initial state.
It is guaranteed that the sum of n\textstyle nn over all test cases does not exceed 5×105\textstyle 5\times 10^55×105.
输出描述:
For each test case, output one integer on a single line, representing the minimum range of the array after any number of operations.
示例1
输入
3 4 2 4 5 3 3 1 2 100 1 10000
输出
1 1 0
题目大意
给两种操作:排序数组后在值域上翻折一个前缀或翻折一个后缀,问任意次操作能做到的最小极差
解题思路
先直接给出答案。将输入数组 排序,则答案为ans=gcd(a2-a1,a3-a2....an-an-1);
由题可知数据只有一种时答案只会出0,因此要特判
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 7;
int a[N];
void slove() {
int n;
cin >> n;
for (int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + 1 + n);
if (n == 1) {
cout << 0 << endl;
return;
}
//根据样例很容易推出ans=gcd(a2-a1,a3-a2....an-an-1)
int s1 = a[2] - a[1];
for (int i = 3; i <= n; i++) {
s1 = __gcd(s1, a[i] - a[i - 1]);
}
cout << s1 << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
slove();
return 0;
}