二次筛选区间素数 POJ2689

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 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.
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 const int maxn=50000+10;
 6 using namespace std;
 7 int p[maxn],a[maxn],f[1000000+10],t=0;
 8 void prepare(){
 9     int m=(int)sqrt(50000);
10     for(int i=2;i<=m;i++)
11         if(!p[i])
12             for(int j=i*i;j<=50000;j+=i)p[j]=1;
13     for(int i=2;i<=50000;i++)if(!p[i])a[++t]=i;
14 }
15 int main(){
16     prepare();
17     int L,U;
18     while(scanf("%d%d",&L,&U)!=EOF){
19         if(L<2)L=2;
20         memset(f,0,sizeof(f));
21         for(int i=1;i<=t;i++){
22             int s=(L)/a[i],t=U/a[i];
23             if(L%a[i])s++;     //这步是必须的,不然会出现j*a[i]-L<0 而出现runtime error
24             if(s<2)s=2;
25             for(int j=s;j<=t;j++)f[j*a[i]-L]=1;
26         }
27         int p=-1,max_ans=-1,min_ans=1000000000,x1,y1,x2,y2;
28         for(int i=0;i<=U-L;i++){
29             if(f[i]==0){
30                 if(p==-1){p=i;continue;}
31                 if(max_ans<i-p){max_ans=i-p;x1=p+L;y1=i+L;}
32                 if(min_ans>i-p){min_ans=i-p;x2=p+L;y2=i+L;}
33                 p=i;
34             }
35         }
36         if(max_ans==-1)cout<<"There are no adjacent primes."<<endl;
37         else cout<<x2<<","<<y2<<" are closest, "<<x1<<","<<y1<<" are most distant."<<endl;
38     }
39     return 0;
40 }
时间: 2024-11-03 22:37:49

二次筛选区间素数 POJ2689的相关文章

CodeForces114E——Double Happiness(素数二次筛选)

Double Happiness On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number t is his lucky number, if it can be represented as:t

LightOJ 1197 LightOJ 1197(大区间素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1197 题目大意: 就是给你一个区间[a,b]让你求这个区间素数的个数 但a.b的值太大没法直接进行素数筛选(没法开那么大的数组),我们可以将a当做0,将b当做b-a 这样求[a,b]之间就变成了求[0, b - a]之间,这样就可以开数组来筛选 下图是代码式子j = j + prime[i] - a % prime[i]的由来 #include<stdio.h> #include<ma

LightOj 1197 Help Hanzo (区间素数筛选)

题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围比较大,无法把这个区间内的所以素数全部筛选出来,但是b-a这个区间比较小,所以可以用区间素数筛选的办法解决这个题目. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<

POJ 2689 二次筛选(映射)

Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12303   Accepted: 3296 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

hdu6069(简单数学+区间素数晒法)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意: 给出 l, r, k.求:(lambda d(i^k))mod998244353,其中 l <= i <= r, d(i) 为 i 的因子个数. 思路:若 x 分解成质因子乘积的形式为 x = p1^a1 * p2^a2 * ... * pn^an,那么 d(x) = (a1 + 1) * (a2 + 1) * ... * (an + 1) .显然 d(x^k) = (a1 * k

UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)

UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂) 题目链接 题目大意:如果有一个合数,然后它满足任意大于1小于n的整数a, 满足a^n%n = a;这样的合数叫做Carmichael Numbers.题目给你n,然你判断是不是Carmichael Numbers. 解题思路:首先用筛选法构造素数表,判断n是否是合数,然后在用快速幂求a^2-a^(n - 1)是否满足上述的式子.快速幂的时候最好用long long ,防止相乘溢出. 代码: #include <

light_oj 1197 区间素数筛

light_oj 1197 区间素数筛 M - Help Hanzo Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1197 Description Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason

素数的计算-埃氏筛法(区间素数利器)

素数,各种素数,各种题总是遇到素数. 下面我们来说一下求素数的一种比较有效的算法. 就是筛法.因为这个要求得1-n区间的素数只需要O(nloglogn)的时间复杂度. 下面来说一下它的思路. 思路:现在又1-n的数字,素数嘛就是除了1和本身之外没有其他的约数,所以有约数的都不是素数. 我们从2开始往后遍历,是2的倍数的都不是素数,所以我们把他们划掉 然后现在从2往后就是3了 因为3的前面没有能整除3的,所以3是素数,然后3的倍数全都不是素数,我们接着划掉. 然后就是5了,因为4是2的倍数不是素数

区间素数筛模版

区间素数筛模版 筛出区间[a,b]的素数.(b-a<=10000,1<=a<=b<=2^31) 存在P中,素数个数即为P的size(). ll a,b; bool isprime[maxn]; vector<ll> prime; bool isP[maxn]; vector<ll> P; void play_prime() { memset(isprime,1,sizeof(isprime)); isprime[1]=0; for(int i=2;i<