solution.cpp 1.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;

typedef long long LL;
set<int> all;

bool isPrimt(LL t)
{ //判断是否是素数
    for (int i = 2; i < t / 2; ++i)
    {
        if (t % i == 0)
            return false;
    }
    return true;
}

int f(LL a[], int n)
{ //寻找长度为10的等差素数列
    for (int i = 0; i < n; ++i)
    { //枚举首项
        LL first = a[i];
        for (int delta = 1; delta < a[n - 1] - first; ++delta)
        { //枚举公差
            int m = first;
            for (int j = 1; j < 10; ++j)
            { //枚举个数
                m += delta;
                if (all.find(m) == all.end())
                    break; //m不是素数
                if (m > a[n - 1])
                    break;
                if (j == 9) //已经找了10项
                    return delta;
            }
        }
    }
    return -1;
}
const int N = 5000;

int main()
{
    LL a[N];

    a[0] = 2;
    a[1] = 3;
    all.insert(2);
    all.insert(3);

    int index = 2;
    LL t = 5;
    while (index < N)
    { //枚举5000位素数表
        if (isPrimt(t))
        {
            a[index++] = t;
            all.insert(t);
        }
        t++;
    }

    printf("%d\n", f(a, N));
    return 0;
}