POJ 2689 Prime Distance

本文介绍了一种高效算法,用于在指定范围内寻找距离最近及最远的相邻素数对。通过优化的筛法,包括欧拉筛法和埃氏筛法的结合使用,解决了大数据量下的素数对查找问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input

Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.
Output

For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.
Sample Input

2 17
14 17
Sample Output

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

题意
在L~R(含端点)的区间内找出距离最近的一对素数和距离最远的一对素数


思路
数据量比较大,常规筛法解决不了。优化一下。 题目保证R-L<=1000000,统计这个区间的素数就行,先找到这个区间所有的数的最小素因子,即用欧拉筛法筛到sqrt(2^31)就行了,再用得到的素数加上埃氏筛法筛出L~R区间内的素数。之后对该区间的素数扫描一遍,记录距离最近的一对素数和距离最远的一对素数。
#include<cstdio>
#include<cstring>
#define N 50000
#define DIS 1000005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long int LL;
int prime[N];
int prime2[DIS];
int isnotprime[DIS];
void EulerSeiv()   //欧拉筛法
{
    for(int i=2; i<N; i++)   
    {
        if(!prime[i])
            prime[++prime[0]]=i;   //prime[0]记录该数组有多少个数
        for(int j=1; j<=prime[0]&&i*prime[j]<N; j++)
        {
            prime[i*prime[j]]=true;   
            if(i%prime[j]==0)
                break;
        }
    }
}
void GetPrime(int L,int R)
{
    memset(isnotprime,0,sizeof(isnotprime));  // 标记数组
    if(L<2)
        L=2;
    for(int i=1; i<prime[0]&&(LL)prime[i]*prime[i]<=R; i++) // 加long long 防溢出
    {
        int s=L/prime[i]+(L%prime[i]!=0);      //向上取整
        if(s==1)     
            s=2;                               // 防止把prime数组中的数筛掉
        for(int j=s; (LL)j*prime[i]<=R; j++)
            if((LL)j*prime[i]>=L)              // 防止下标越界
                isnotprime[j*prime[i]-L]=true; // 标记该区间不是素数的数,压缩空间,或者理解为偏移处理,偏移量为a   
    }
    prime2[0]=0;                               
    for(int j=0; j<=R-L; j++)                  // 统计该区间的素数
        if(!isnotprime[j])
            prime2[++prime2[0]]=j+L;           
}

int main()
{
    int L,R;
    EulerSeiv();
    while(scanf("%d%d",&L,&R)!=EOF)
    {
        GetPrime(L,R);
        int Lmax=0,Rmax=0,Lmin=0,Rmin=INF,dis;
        if(prime2[0]<2)
            puts("There are no adjacent primes.");
        else
        {
            for(int i=2; i<=prime2[0]; i++)
            {
                dis=prime2[i]-prime2[i-1];
                if(Rmin-Lmin>dis)
                {
                    Rmin=prime2[i];
                    Lmin=prime2[i-1];
                }
                if(Rmax-Lmax<dis)
                {
                    Rmax=prime2[i];
                    Lmax=prime2[i-1];
                }
            }
            printf("%d,%d are closest, %d,%d are most distant.\n",Lmin,Rmin,Lmax,Rmax);
        }
    }
    return 0;
}

摘抄于该大佬的博客:here

标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值