本文参考于: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)内的素数了。
有的时候需要求出某个特定区间的素数,但是数可能很大,数组也要开不小,所以需要进行下标转移,这样才可以使用筛选法。
#define ll long long
const int maxn = 1000010;
bool pri[maxn],prii[maxn];
ll prime[maxn];
int cnt;
//区间[a,b)内的整数筛法,prii[i-a]=true---代表i是素数,注意这里下标偏移了a,所以从0开始
//求解[a,b]内,就把 < 全改为 <=
void seg_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++){ //统计,去1
if(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 Distance http://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>
#define ll long long
#define ld long double
#define ull unsigned long long
using namespace std;
const int maxn = 1000010;
const int inf = 0x3f3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f;
bool pri[maxn],prii[maxn];
ll prime[maxn],n,m;
int cnt;
void seg_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 , 不然 RE
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++){
if(pri[i]&&i+a>1) prime[cnt++] = i+a; //注意去1
}
}
int main(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(int i=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);
}
return 0;
}