D2. Half of Same- Codeforces Round #748 (Div. 3)

原题链接https://codeforces.com/contest/1593/problem/D2

D2. Half of Same

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This problem is a complicated version of D1, but it has significant differences, so read the whole statement.

Polycarp has an array of nn (nn is even) integers a1,a2,…,ana1,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≤n1≤i≤n) and reduce the number aiai by kk.

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 kk at which such a situation is possible, or print −1−1 if such a number can be arbitrarily large.

Input

The first line contains one integer tt (1≤t≤101≤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≤404≤n≤40) (nn is even). The second line contains nn integers a1,a2,…ana1,a2,…an (−106≤ai≤106−106≤ai≤106).

It is guaranteed that the sum of all nn specified in the given test cases does not exceed 100100.

Output

For each test case output on a separate line an integer kk (k≥1k≥1) — the maximum possible number that Polycarp used in operations on the array, or −1−1, if such a number can be arbitrarily large.

Example

input

Copy

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

Copy

13
2
-1
-1

-------------------------------------------------------------------------------------------------------------------

可以根据上一题推断,如果有超过n/2元素相同,那么k可以取任意值

上一题是求n个数和最小值的差值

本题想要构造出n/2个,那么n/2个元素和其最小值差距也必须是k的倍数

一种方法是,我们可以枚举n/2个数,然后按照第一题方法写,显然会超时

另一种方法,我们枚举最小值,计算剩余数与它的差值,然后取最大gcd,然而这种做法求得的是

剩余n-i个元素和最小值的差,随着元素个数增加,gcd会减小,而我们只需要n/2个元素就够,显然这样做又难以避免枚举n/2个数

比较好的做法是,枚举最小值a[i],计算每一个比它大的元素的差值,提取这个差值的因子,如果这个因子是n/2-1个数与a[i]的差值,那么显然这个k满足条件,并且我们记录k的最大值即可

# include<iostream>
# include<map>
# include<algorithm>
using namespace std;
typedef long long int ll;

map<int,int>m;
int a[110],n;
bool check(int factor,int pos)
{
    int cnt=0;
    for(int i=pos+1;i<=n;i++)
    {
        if((a[i]-a[pos])%factor==0)
            cnt++;
    }

    if(cnt>=n/2-1)
        return 1;
    return 0;
}

int main ()
{

    int t;
    cin>>t;

    while(t--)
    {
        int flag=0;
        cin>>n;

        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            m[a[i]]++;
            if(m[a[i]]>=n/2)
                flag=1;
        }
        m.clear();
        if(flag)
        {
            cout<<-1<<'\n';
            continue;
        }


        sort(a+1,a+1+n);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                int cha=a[j]-a[i];

                for(int k=1;k*k<=cha;k++)
                {
                       if(cha%k==0)
                       {
                           if(check(k,i))
                            ans=max(ans,k);
                           if(check(cha/k,i))
                              ans=max(ans,cha/k);

                       }
                }
            }
        }

        cout<<ans<<'\n';
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值