poj2689

来源:互联网 发布:cocos jscompile 源码 编辑:程序博客网 时间:2024/05/20 21:59
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 1714 17
Sample Output
2,3 are closest, 7,11 are most distant.There are no adjacent primes. 



题意:输入两个数:L,U  要求输入这个区间内相邻的两个素数中作差最小的素数对以及最大的素数对

两次筛选。因为L,U的值过大,但是U和L的差值只有1e6,并且对于int范围内的合数来说,最小质因子必定小于2^16。所以可以求出[l,u]中合数,转而求出素数,然后暴力枚举所有素数对。

先求出50000以内的素数,然后找出l,u区间内的合数(一定是其倍数),遍历

a = (l-1)/prime[i]+1;b = u/prime[i];

(a,b)区间就可以找出l,u之间所有含有prime【】的合数,做个标记,最后枚举50000以内的素数

#include <iostream>#include <math.h>#include <stdio.h>#include <string.h>using namespace std;const int AX = 5e4;const int maxn = 1e6+666;const int INF = 1e9;int vis[AX];int f[maxn];int prime[AX];int ans;void prime1(){                        //筛选出50000以内的素数ans = 0;memset(vis,0,sizeof(vis));int q = (int)sqrt(AX+0.5);for(int i=2;i<=q;i++){if(!vis[i]){for(int j= i*i;j<AX;j+=i){vis[j] = 1;}}}for(int i=2;i<AX;i++){if(!vis[i]){prime[ans++] = i;}}}void prime_2(int l,int u){           //求l,u合数int a,b;for(int i=0;i<ans;i++){a = (l-1)/prime[i]+1;b = u/prime[i];for(int j=a;j<=b;j++){if(j>1)  f[j*prime[i]-l] = 1;        //偏移减去l,后面再加回来。}}}int main(){long long l,u;prime1();while(cin>>l>>u){memset(f,0,sizeof(f));if(l == 1) l = 2;prime_2(l,u);int mi = INF;int ma = -1;int p = -1;int x1,x2,y1,y2;for(int i=0;i<=u-l;i++){          //枚举50000以内素数if(!f[i]){if(p == -1) {p=i;continue;}if(mi > i-p) mi = i-p,x1=p+l,y1=i+l;     //偏移量加回来if(ma < i-p) ma = i-p,x2=p+l,y2=i+l;p = i;}}if(ma == -1) printf("There are no adjacent primes.\n");else cout<<x1<<","<<y1<<" are closest, "<<x2<<","<<y2<<" are most distant."<<endl;}return 0;}



原创粉丝点击