题目链接:Problem - D1 - Codeforces
This problem is a simplified version of D2, but it has significant differences, so read the whole statement.
Polycarp has an array of nn (nn is even) integers a1,a2,…,an Polycarp conceived of a positive integer kk. After that, Polycarp began performing the following operations on the array: take an index ii (1≤i≤n) and reduce the number aiai by k.
After Polycarp performed some (possibly zero) number of such operations, it turned out that all numbers in the array became the same. Find the maximum k at which such a situation is possible, or print −1 if such a number can be arbitrarily large.
Input
The first line contains one integer t 1≤t≤10) — the number of test cases. Then tt test cases follow.
Each test case consists of two lines. The first line contains an even integer nn (4≤n≤40) (nn is even). The second line contains nn integers a1,a2,…an(−10^6≤ai≤10^6).
It is guaranteed that the sum of all nn specified in the given test cases does not exceed 100.
Output
For each test case output on a separate line an integer kk (k≥1) — the maximum possible number that Polycarp used in operations on the array, or −1, if such a number can be arbitrarily large.
Example
input
Copy
3 6 1 5 3 1 1 5 8 -1 0 1 -1 0 1 -1 0 4 100 -1000 -1000 -1000
output
Copy
2 1 1100
题意:可以选择一个值-k(任意次,可以0次),然后问把所有数字变成相同数字的最大k, 如果k可以为任意数出-1
思路:找到最小值,然后出去最小值的其他值-最小值的值求公因数,如果刚开始的值都相同就出-1
#include<bits/stdc++.h>
using namespace std;
int arr[45];
int main(){
int t;
cin >> t;
while(t--){
int n;
int maxn = -1000005;
int minn = 1000005;
cin >> n;
for(int i = 0; i < n; i++){
cin >> arr[i];
minn = min(minn, arr[i]);
maxn = max(maxn, arr[i]);
}
int ans = maxn - minn;
for(int i = 0; i < n; i++){
if(arr[i] != minn){
ans = __gcd(ans, arr[i] - minn);
}
}
if(ans == 0){
cout << -1 << endl;
continue;
}
cout << ans << endl;
}
return 0;
}