描述
题解
比赛时,这个问题不用多想,直接暴力枚举即可,但是赛后想着用非暴力解试试看,挺好的一道题。
既然要求余数都不一样,那么我们不如反过来想,如果一样时满足什么,这样找不满足这个条件的不就好了?
假设  a  和    
对于一小部分数据,可能直接暴力的枚举比预处理后再枚举会更快一些,但是对于大数据的时候,还是预处理一下比较好,那么,对于这道题,数据不大,我们大可以直接暴力枚举,何乐而不为呢?
代码
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN = 3000;
const int MAXM = 2e4 + 50;
int n;
int A[MAXN];
int hash_[MAXM];
bool charge(int x)
{
    for (int i = x; i < MAXM; i += x)
    {
        if (hash_[i])
        {
            return false;
        }
    }
    return true;
}
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        memset(hash_, 0, sizeof(hash_));
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &A[i]);
        }
        for (int i = 1; i < n; i++)
        {
            for (int j = i + 1; j <= n; j++)
            {
                if (A[i] > A[j])
                {
                    hash_[A[i] - A[j]] = 1;
                }
                else
                {
                    hash_[A[j] - A[i]] = 1;
                }
            }
        }
        for (int ans = n; ; ans++)
        {
            if (charge(ans))
            {
                printf("%d\n", ans);
                break;
            }
        }
    }
    return 0;
}
京公网安备 11010502036488号