码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10140 - Prime Distance(数论)

时间:2014-07-03 16:00:49      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   for   io   

题目链接:uva 10140 - Prime Distance

题目大意:给出一个范围,问说该范围内,相邻的两个素数最大距离和最小距离。

解题思路:类似素数筛选法,起始位置有L开始,直到超过R,处理出素数之后就好办了。

#include <cstdio>
#include <cstring>
#include <cmath>

const int maxn = 1e6;
typedef long long ll;

int cp, v[maxn+5];
ll limit, prime[maxn+5];

void primeTable (int n) {
    cp = 0;
    memset(v, 0, sizeof(v));    

    for (int i = 2; i <= n; i++) {
        if (v[i])
            continue;

        prime[cp++] = i;
        for (int j = 2 * i; j <= n; j += i)
            v[j] = 1;
    }
}

inline ll cal(ll a, ll b) {
    ll k = b / a;
    if (b%a)
        k++;

    if (k * a <= limit && v[k*a] == 0)
        k++;
    return k * a;
}

int cnt, set[maxn+5], vis[maxn+5];
ll L, R;

void solve () {
    memset(vis, 0, sizeof(vis));

    ll m = sqrt(R+0.5);

    for (int i = 0; i < cp; i++) {

        if (prime[i] > m)
            break;

        for (ll j = cal(prime[i], L); j <= R; j += prime[i]) {
            vis[j-L] = 1;
        }
    }

    cnt = 0;
    for (ll i = L; i <= R; i++) {
        if (i == 1 || i == 0)
            continue;

        if (vis[i-L])
            continue;
        set[cnt++] = i-L;
    }
}

int main () {
    limit = 1<<16;
    primeTable(limit);
    while (scanf("%lld%lld", &L, &R) == 2) {
        solve();

        if (cnt <= 1)
            printf("There are no adjacent primes.\n");
        else {
            int minRec, maxRec, minDis, maxDis;
            maxDis = 0;
            minDis = 1e7;

            for (int i = 1; i < cnt; i++) {
                int tmp = set[i] - set[i-1];

                if (tmp > maxDis) {
                    maxDis = tmp;
                    maxRec = i;
                }

                if (tmp < minDis) {
                    minDis = tmp;
                    minRec = i;
                }
            }
            printf("%lld,%lld are closest, %lld,%lld are most distant.\n", set[minRec-1]+L, set[minRec]+L, set[maxRec-1]+L, set[maxRec]+L);
        }
    }
    return 0;
}

uva 10140 - Prime Distance(数论),布布扣,bubuko.com

uva 10140 - Prime Distance(数论)

标签:style   http   color   os   for   io   

原文地址:http://blog.csdn.net/keshuai19940722/article/details/36453513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!