news 2026/7/28 13:57:38

区间素数筛法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
区间素数筛法

本文参考于:https://www.cnblogs.com/nowandforever/p/4515612.html

类似问法:

给定整数a和b,请问区间[a,b)内有多少素数?
a < b <= 1012
b - a<= 106
因为b以内的合数的最小质因数一定不是超过sqrt(b),如果有sqrt(b)以内的素数表的话,就可以把筛选法用在[a,b)上了,先分别做好[2,sqrt(b))的表和[a,b)的表,然后从[2,sqrt(b))的表中筛得素数的同时,也将其倍数从[a,b)的表中划去,最后剩下的就是区间[a,b)内的素数了。
有的时候需要求出某个特定区间的素数,但是数可能很大,数组也要开不小,所以需要进行下标转移,这样才可以使用筛选法。

#definell long longconstintmaxn=1000010;boolpri[maxn],prii[maxn];ll prime[maxn];intcnt;//区间[a,b)内的整数筛法,prii[i-a]=true---代表i是素数,注意这里下标偏移了a,所以从0开始//求解[a,b]内,就把 < 全改为 <=voidseg_pri(ll a,ll b){cnt=0;memset(pri,false,sizeof(pri));for(ll i=0;i*i<b;i++)prii[i]=true;//对[2,sqrt(b))的初始化全为素数for(ll i=0;i<b-a;i++)pri[i]=true;//求下表偏移后的[a,b)进行初始化for(ll i=2;i*i<b;i++){if(prii[i]){for(ll j=2*i;j*j<b;j+=i)prii[j]=false;//筛选[2,sqrt(b))//(a+i-1)/i得到最接近a的i的倍数,最低是i的2倍,然后筛选for(ll j=max(2ll,(a+i-1)/i)*i;j<b;j+=i)pri[j-a]=false;}}for(ll i=0;i<b-a;i++){//统计,去1if(pri[i]&&i+a>1)prime[cnt++]=i+a;}/*for(int i=0;i<cnt;i++) cout<<prime[i]<<endl; cout<<cnt<<endl;*/}
传送门:POJ 2689 Prime Distancehttp://poj.org/problem?id=2689

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 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,U(1<=L< U<=2,147,483,647),在[L,U]区间内找相邻素数之差最小的素数对,最大的素数对,并输出(相同者,取第一组),如果区间内素数个数 < 2 ,输出 There are no adjacent primes.

思路

先区间筛得到素数表,在跑一边素数表,找就可以了

#include<cstdio>#include<iostream>#include<cmath>#include<algorithm>#include<cstring>#include<cstdlib>#include<cctype>#include<vector>#include<stack>#include<queue>#include<ctime>#definell long long#defineld long double#defineull unsigned long longusingnamespacestd;constintmaxn=1000010;constintinf=0x3f3f3f3f3f;constll lnf=0x3f3f3f3f3f3f3f;boolpri[maxn],prii[maxn];ll prime[maxn],n,m;intcnt;voidseg_pri(ll a,ll b){cnt=0;memset(pri,false,sizeof(pri));for(ll i=0;i*i<=b;i++)prii[i]=true;for(ll i=0;i<=b-a;i++)pri[i]=true;for(ll i=2;i*i<=b;i++){if(prii[i]){for(ll j=2*i;j*j<=b;j+=i)prii[j]=false;//用 ll , 不然 REfor(ll j=max(2ll,(a+i-1)/i)*i;j<=b;j+=i)pri[j-a]=false;}}for(ll i=0;i<=b-a;i++){if(pri[i]&&i+a>1)prime[cnt++]=i+a;//注意去1}}intmain(void){ll mi,mil,mir;ll ma,mal,mar;while(~scanf("%lld%lld",&n,&m)){seg_pri(n,m);mi=lnf,ma=0;if(cnt<2){puts("There are no adjacent primes.");continue;}for(inti=1;i<cnt;i++){ll d=prime[i]-prime[i-1];if(d<mi){mi=d;mil=prime[i-1];mir=prime[i];}if(d>ma){ma=d;mal=prime[i-1];mar=prime[i];}}printf("%lld,%lld are closest, ",mil,mir);printf("%lld,%lld are most distant.\n",mal,mar);}return0;}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/28 13:56:25

计算机毕业设计之基于SpringBoot的高校请假系统的设计与实现

本文主要探讨基于SpringBoot框架设计与实现的高校请假系统。该系统旨在为高校师生提供一个便捷、高效的请假申请和管理平台。系统的核心功能包括用户注册与登录、学生请假、销假申请、学生考勤以及公告信息等。系统采用SpringBoot作为基础框架&#xff0c;利用其快速开发和简化…

作者头像 李华
网站建设 2026/7/28 13:56:01

IL-2对B细胞功能的双向调控:从促分化到抗炎稳态的机制解析

简述&#xff1a; 本文系统阐述白细胞介素-2&#xff08;IL-2&#xff09;通过其受体复合体对B细胞进行直接调控的分子机制&#xff0c;分析IL-2在促进浆细胞分化与限制过度体液应答中的双向功能&#xff0c;探讨其在诱导IL-10分泌型调节性B细胞中的关键作用&#xff0c;并介绍…

作者头像 李华
网站建设 2026/7/28 13:54:54

液态与膏状助焊剂:电子焊接核心材料对比与应用

1. 液态助焊剂与膏状助焊剂的本质区别 在电子焊接领域&#xff0c;助焊剂的选择直接影响焊接质量和效率。液态助焊剂&#xff08;Liquid Flux&#xff09;和膏状助焊剂&#xff08;Paste Flux&#xff09;是两种最常见的形态&#xff0c;它们的物理特性决定了完全不同的应用场景…

作者头像 李华
网站建设 2026/7/28 13:50:38

FF14钓鱼计时器终极指南:渔人的直感助你轻松钓获鱼王

FF14钓鱼计时器终极指南&#xff1a;渔人的直感助你轻松钓获鱼王 【免费下载链接】Fishers-Intuition 渔人的直感&#xff0c;最终幻想14钓鱼计时器 项目地址: https://gitcode.com/gh_mirrors/fi/Fishers-Intuition 渔人的直感是一款专为《最终幻想14》玩家设计的智能钓…

作者头像 李华
网站建设 2026/7/28 13:50:03

监控工具选型终极对比:Prometheus vs Datadog vs 自建方案

监控工具选型终极对比&#xff1a;Prometheus vs Datadog vs 自建方案 一、从"看不见"到"看得清"&#xff1a;监控工具选型的纠结 2026 年初&#xff0c;某中型互联网公司在监控工具选型上产生了分歧&#xff1a; 运维团队倾向 Datadog&#xff08;省事&am…

作者头像 李华