Codeforces Round #748 (Div. 3)

比赛链接

Codeforces Round #748 (Div. 3)

D2. Half of Same

Polycarp has an array of \(n\left(n\right.\) is even) integers \(a_{1}, a_{2}, \ldots, a_{n}\). Polycarp conceived of a positive integer \(k\). After that, Polycarp began performing the following operations on the array: take an index \(i(1 \leq i \leq n)\) and reduce the number \(a_{i}\) by \(k\).

After Polycarp performed some (possibly zero) number of such operations, it turned out that at least half of the 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 \leq t \leq 10)\) - the number of test cases. Then \(t\) test cases follow.
Each test case consists of two lines. The first line contains an even integer \(n(4 \leq n \leq 40)\) ( \(n\) is even). The second line contains \(n\) integers \(a_{1}, a_{2}, \ldots a_{n}\left(-10^{6} \leq a_{i} \leq 10^{6}\right)\).
It is guaranteed that the sum of all \(n\) specified in the given test cases does not exceed \(100\) .

Output

For each test case output on a separate line an integer \(k(k \geq 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

4
6
48 13 22 -15 16 35
8
-1 0 1 -1 0 1 -1 0
4
100 -1000 -1000 -1000
4
1 1 1 1

output

13
2
-1
-1

解题思路

思维

显然,对于选出来的任意两个数 \(a,b\),有 \(a\%k=b\%k\),即 \((a-b)\%k=0\),可以枚举选出来的最小值,将所有数减去这个最小值,这些数对 \(k\) 取模的模数都为 \(0\),即枚举所有差值的因子,如果其出现的次数大于等于 \(n/2\),则该因子满足条件,此时更新答案,注意,如果出现其他数和最小值相等,则该数必选,因此时差值为 \(0\),肯定满足条件

  • 时间复杂度:\(O(n^2\times max(\{a_i\}))\)

代码

// Problem: D2. Half of Same
// Contest: Codeforces - Codeforces Round #748 (Div. 3)
// URL: https://codeforces.com/contest/1593/problem/D2
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=45;
int t,n,a[N];
vector<int> Div(int n)
{
	vector<int> res;
	for(int i=1;i<=n/i;i++)
		if(n%i==0)
		{
			res.pb(i);
			if(i*i!=n)
				res.pb(n/i);
		}
	return res;
}
int main()
{
	help;
    for(cin>>t;t;t--)
    {
    	cin>>n;
    	for(int i=1;i<=n;i++)cin>>a[i];
    	sort(a+1,a+1+n);
    	bool f=false;
    	for(int i=1;i+n/2-1<=n;i++)
    		if(a[i]==a[i+n/2-1])
    		{
    			f=true;
    			break;
    		}
    	if(f)
    	{
    		puts("-1");
    		continue;
    	}
    	int res=0;
    	for(int i=1;i<=n;i++)
    	{
    		vector<int> b;
    		int tt=0;
    		for(int j=1;j<=n;j++)
    			if(a[j]>a[i])b.pb(a[j]-a[i]);
    			else if(a[i]==a[j])tt++;
    		unordered_map<int,int> mp;
    		for(int i:b)
    			for(int j:Div(i))
    				mp[j]++;
    		for(auto t:mp)
    			if(t.se>=n/2-tt)res=max(res,t.fi);
    	}
    	cout<<res<<'\n';
    }
    return 0;
}
posted @ 2022-05-16 11:09  zyy2001  阅读(27)  评论(0编辑  收藏  举报