Codeforces Round #748 (Div. 3)

比赛链接

Codeforces Round #748 (Div. 3)

D2. Half of Same

Polycarp has an array of n(n is even) integers a1,a2,,an. Polycarp conceived of a positive integer k. After that, Polycarp began performing the following operations on the array: take an index i(1in) and reduce the number ai 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(1t10) - 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(4n40) ( n is even). The second line contains n integers a1,a2,an(106ai106).
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(k1) - 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,即 (ab)%k=0,可以枚举选出来的最小值,将所有数减去这个最小值,这些数对 k 取模的模数都为 0,即枚举所有差值的因子,如果其出现的次数大于等于 n/2,则该因子满足条件,此时更新答案,注意,如果出现其他数和最小值相等,则该数必选,因此时差值为 0,肯定满足条件

  • 时间复杂度:O(n2×max({ai}))

代码

// 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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16276209.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示