0x31 prime distance(质数)

题目描述:

给定两个整数L和U,你需要在闭区间[L,U]内找到距离最接近的两个相邻质数C1和C2(即C2-C1是最小的),如果存在相同距离的其他相邻质数对,则输出第一对。

同时,你还需要找到距离最远的两个相邻质数D1和D2(即D1-D2是最大的),如果存在相同距离的其他相邻质数对,则输出第一对。

输入格式:

每行输入两个整数L和U,其中L和U的差值不会超过1000000。

输出格式:

对于每个L和U ,输出一个结果,结果占一行。

结果包括距离最近的相邻质数对和距离最远的相邻质数对。(具体格式参照样例)

如果L和U之间不存在质数对,则输出“There are no adjacent primes.”。

数据范围:

1≤L<U≤231-1,R-L≤106

输入样例:

2 17

14 17

输出样例:

2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

思路:

①用筛法求出2~sqrt(R)之间的所有质数。

②标记i*p(ceil(L/p)≤i≤floor(R/p))。

③所有未标记的数就是[L,R]中的质数。对相邻的质数两两比较,找出差最大的。

时间复杂度:O(∑质数p≤sqrt(R)(R-L)/p)=O(sqrt(R)log log sqrt(R)+(R-L)log log R)。

代码如下:

#include<bits/stdc++.h>
#define N 1000000+10
#define M 100000+10
using namespace std;

int l,r,m;
int v[M],prime[M];
bool st[N];

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 main(){
    primes(100000);//step 1 

    while(cin>>l>>r){
        memset(st,true,sizeof(st));
        if(l==1)st[0]=false;

        for(int i=1;prime[i]<=sqrt(r);i++){
            int p=prime[i];

            int l1,r1;
            if(l%p==0)l1=l/p;
            else l1=l/p+1;
            r1=floor(r/p);

            for(int j=max(2,l1);j<=r1;j++)
                st[j*p-l]=false;//step 2
        }

        int min1=-1,min2=-1,max1=-1,max2=-1;
        int minn=1e9,maxx=-1;
        int before;
        bool t=false;

        for(int i=0;i<=r-l;i++){
            if(!st[i])continue;

            if(!t){
                before=i;t=true;
            }

            else{
                if(i-before>maxx){
                    max1=before;max2=i;
                    maxx=i-before;
                }

                if(i-before<minn){
                    min1=before;min2=i;
                    minn=i-before;
                }

                before=i;
            }
        }//step 3

        if(min1==-1)cout<<"There are no adjacent primes."<<endl;
        else{
            cout<<min1+l<<","<<min2+l<<" are closest, "
                <<max1+l<<","<<max2+l<<" are most distant."<<endl;
        }
    }

    return 0;
} 

原文地址:https://www.cnblogs.com/zhengchang/p/prime_distance.html

时间: 2024-11-06 03:38:18

0x31 prime distance(质数)的相关文章

POJ-2689 Prime Distance(线性筛法)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17021   Accepted: 4536 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

poj 2689 Prime Distance 【数论】【筛法求素数】

题目链接:传送门 题目大意: 给你L和R两组数,L和R的范围是2^32,其间隔(即R-L最大为1,000,000.) .让你求出L和R之间素数的最大间隔和最小的间隔. 比如 2 17.之间的最小素数间隔是2 3,最大的素数间隔是11 17. 要是直接进行一个2^32次方筛法然后在判断是会T的. 我们这样来想,筛法求素数的原理是什么: /**vis数组标记为0则说明是素数*/ int vis[10005]; void getPrimevis(int n) { int m=sqrt(n+0.5);

[ACM] POJ 2689 Prime Distance (筛选范围大素数)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

UVA 10140 - Prime Distance(数论)

10140 - Prime Distance 题目链接 题意:求[l,r]区间内最近和最远的素数对. 思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案.注意1的情况,一开始没判断1,结果WA了 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define INF 0x3f

POJ 2689 Prime Distance 素数筛选法应用

题目来源:POJ 2689 Prime Distance 题意:给出一个区间L R 区间内的距离最远和最近的2个素数 并且是相邻的 R-L <= 1000000 但是L和R会很大 思路:一般素数筛选法是拿一个素数 然后它的2倍3倍4倍...都不是 然后这题可以直接从2的L/2倍开始它的L/2+1倍L/2+2倍...都不是素数 首先筛选出一些素数 然后在以这些素数为基础 在L-R上在筛一次因为 R-L <= 1000000 可以左移开一个1百万的数组 #include <cstdio>

POJ 2689 Prime Distance

Prime Distance Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a

POJ题目2689 Prime Distance(任何区间素数筛选)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13459   Accepted: 3578 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

POJ-2689 Prime Distance (两重筛素数,区间平移)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th

数论 - 素数的运用 --- poj 2689 : Prime Distance

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number th