#10197. 「一本通 6.2 例 1」Prime Distance(POJ 2689,质数,因子)

在这里插入图片描述
在这里插入图片描述
注意事项:
1、素数筛选时j的范围
2、注意i+1与i-1的选用,防止越界
3、1既不是素数也不是合数

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
typedef long long ll;

//const int INF = 0x3f3f3f3f;
//const int MAXN = ;
const ll maxn = 1000000 + 5;
const ll sqrtu = 46346;
//int n, m;
// int v[maxn], prime[maxn], m;
// void primes(int n) {
//     memset(v, 0, sizeof(v));
//     m = 0;
//     for (int i = 2; i <= n; i++) {
//         if (v[i] == 0) {
//             v[i] = i;
//             prime[++m] = i;
//         }
//         for (int j = 1; j <= m; j++) {
//             if (prime[j] > v[i] || prime[j] > n / i) break;
//             v[i * prime[j]] = prime[j];
//         }
//     }
// }
// int is_prime[sqrtmaxn];

int is_prime[sqrtu], m;
vector<int> primes;
void prime() {
    for (int i = 0; i < sqrtu; i++) {
        is_prime[i] = 1;
    }
    for (int i = 2; i <= sqrt(sqrtu); i++) {
        for (int j = i; j <= sqrtu / i; j++) { //注意j的范围
            if (i * j >= sqrtu) break;
            is_prime[i * j] = 0;
        }
    }
    primes.clear();
    // primes.push_back(1);
    //因子不应该包含1
    primes.push_back(2);
    primes.push_back(3);
    for (int i = 4; i < sqrtu; i++) {
        if (is_prime[i] == 1) primes.push_back(i);
    }
}
int a[maxn]; //is_prime2
int b[maxn], lenb;
void prime2(ll l, ll u) {
    for (int i = 0; i <= u - l; i++) {
        a[i] = 1;
    }
    //mmp,1既不是素数也不是合数
    //??  1怎么处理?
    for (int i = 0; i < primes.size(); i++) {
        ll p = primes[i];
        for (int j = l / p >= 2 ? l / p : 2; j <= u / p + 1; j++) { //同理,合数的因子应该手动排除掉1
            // printf("%ld %ld %ld %ld %ld\n", j, p, j * p, l, u);
            if (j * p < l) continue;
            if (j * p > u) break;
            a[j * p - l] = 0;
            // cout << j * p << endl;
        }
    }
    if (l == 1) a[1 - l] = 0;
    //集中素数
    memset(b, 0, sizeof(b));
    lenb = 0;
    for (int i = 0; i <= u - l; i++)
        if (a[i] == 1) {
            b[lenb++] = i;
            // cout << "found" << i + l << endl;
        }
}

int main() {
#ifdef LOCAL
    freopen("zz_in.txt", "r", stdin);
    freopen("zz_op.txt", "w", stdout);
#endif
    prime();
    // for (int i = 0; i < primes.size(); i++)
    //     cout << primes[i] << endl;
    ll l, u;
    prime();
    while (~scanf("%ld%ld", &l, &u)) {
        prime2(l, u);
        // for (int i = 0; i < lenb; i++) {
        //     cout << b[i] + l << "\t";
        // }
        // cout << endl;
        if (lenb < 2)
            cout << "There are no adjacent primes.\n";
        else {
            ll d1 = b[0] + l, d2 = b[1] + l, disd = d2 - d1;
            ll c1 = b[0] + l, c2 = b[1] + l, disc = c2 - c1;
            for (int i = 1; i < lenb; i++) {
                if (b[i] - b[i - 1] < disc) {
                    disc = b[i] - b[i - 1];
                    c1 = b[i - 1] + l, c2 = b[i] + l;
                }
                if (b[i] - b[i - 1] > disd) {
                    disd = b[i] - b[i - 1];
                    d1 = b[i - 1] + l, d2 = b[i] + l;
                }
                //注意i+1与i-1的选用,防止越界
            }
            // cout << c1 << "\t" << c2 << endl;
            printf("%ld,%ld are closest, %ld,%ld are most distant.\n", c1, c2, d1, d2);
        }
    }

#ifdef LOCAL
    printf("Time used = %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页