prime distance ( 大区间内部质数

# 题意

给定两个整数l,r,求在[l,r]区间内距离最近的一对相邻质数,和距离最远的一对相邻质数

l,r ∈ [1 , 231-1]

其中r-l <= 1e6

# 题解

l,r的范围很大,大约是2e9 线性算法也无法求出[1,r]的所有质数,但是r-l的范围小,

任何一个合数必定会包含一个不超过 √n 的质因数,所以求出 2~√r 的所有质数,

对于每个质数,将[l,r]中能整除这个数字的标记,剩下的就是区间内的质数。

对于质数两两比较求最值即可

复杂度 O (√r * log (log r))

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int N=1e6;
 5 ll primes[N],cnt;
 6 bool st[N];
 7 bool rec[N];
 8 ll pri[N],num;//位于 l~r 之间的质数
 9 void get_primes(int n){
10     memset(st,false,sizeof st);
11     for(int i=2;i<=n;i++){
12         if(!st[i]) primes[cnt++] = i;
13         for(int j=0;primes[j]<=n/i;j++){
14             st[primes[j] * i]=true;
15             if(i % primes[j] ==0 ) break;
16         }
17     }
18 }
19 int main(){
20     ll l,r;
21     while(cin>>l>>r){
22         get_primes(sqrt(r));
23         memset(rec,false,sizeof rec);
24         for(ll i=0;i<cnt;i++){
25             ll p=primes[i];
26             for(ll j = max(2*p,(l+p-1)/p*p);j<=r;j+=p){//第一个大于l的p的倍数,分两种情况,%p=0 和 %p != 0
27                 rec[j-l]=true;
28             }
29         }
30         num=0;
31         for(int i=0;i<=r-l;i++){
32             if(!rec[i] && i+l >1) // 1不是质数特判
33                 pri[num++]=i+l;
34         }
35
36         if(num<2) puts("There are no adjacent primes.");
37         else {
38             int mx=0,mn=0;
39             for(int i=0;i<num-1;i++){
40                 int d = pri[i+1]-pri[i];
41                 if(d > pri[mx+1] - pri[mx]) mx=i;
42                 if(d < pri[mn+1] - pri[mn]) mn=i;
43             }
44             printf("%d,%d are closest, %d,%d are most distant.\n",pri[mn],pri[mn+1],pri[mx],pri[mx+1]);
45         }
46     }
47 }

原文地址:https://www.cnblogs.com/hhyx/p/12601890.html

时间: 2024-08-03 03:37:15

prime distance ( 大区间内部质数的相关文章

POJ - 2689 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 number that is

[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

poj 2689 Prime Distance(大区间筛素数)

http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 因为L<U<=2147483647,直接筛素数是不行的,数组就开不了.但是可以根据素数筛的原理.我们先筛出sqrt(2147483647)以内的素数,然后拿这些素数去筛[L,U]之间的素数,即两次素数筛.但是L,U还是很大,但U-L<=1000000,所以进行区间平移,将[L,U]平移为[0,U-L],就能用数组放得下. #include &l

[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

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

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(线性筛法)

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 素数筛选法应用

题目来源: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>