poj 2689 l-r区间素数对

来源:互联网 发布:数据库视频教学 编辑:程序博客网 时间:2024/06/06 04:26

传送门


Prime Distance
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18956 Accepted: 5077

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 1714 17

Sample Output

2,3 are closest, 7,11 are most distant.There are no adjacent primes.

Source

Waterloo local 1998.10.17

题意:输入区间[l,u],其中l和u为int范围的整数,区间最大为1000000。求出[l,u]中,相邻素数只差最大和最小的素数对。当存在多个时,输出较小的素数对。

网上题解:l,u范围太大,不能直接求int范围的素数。而区间间隔比较小,只有1e6,而且对于int范围内的合数来说,最小质因子必定小于2^16。所以可以求出[l,u]中合数,转而求出素数,然后暴力枚举所有素数对即可。

如何求区间[l,u]中的合数:上面已经说了,合数的最小质因子小于2^16,即小于50000。所以先求出小于50000的所有素数。则区间[l,u]中的合数,必定可以表示为小于50000的素数的倍数。对于素数p来说,令a=(l-1)/p+1,b=u/p。则枚举j=a到b,j*p可以枚举所有[l,u]中质因子含有p的合数。枚举所有小于50000的素数,然后用上述方式枚举倍数,即可找出[l,u]中所有的合数。

由于l,u在int范围,所以不能直接用数组标记。需要加个偏移量,取l,则数组大小小于1e6的f[0,u-l],即可标记。

接着枚举区间中所有的相邻素数对即可。

特别注意:由于1不是小于50000的素数的倍数,所以在与合数相斥中,会被当成素数。需要特别处理下。

#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <time.h>#include <map>#include <algorithm>using namespace std;#define pi acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=1000;const int maxx=1e6+100;const double EPS=1e-7;const int mod=998244353;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template<class T> void Scan(T &x){    static int CH;    while((CH=getchar())<'0'||CH>'9');    for(x=CH-'0';(CH=getchar())>='0'&&CH<='9';x=(x<<3)+(x<<1)+(CH-'0'));}LL pr[maxx];int pri[maxx],val=0,vis[maxx];void init(){    for(int i=2;i<maxx;i++)    {        if(!vis[i])pri[val++]=i;        for(int j=0;j<val&&i*pri[j]<maxx;j++)        {            vis[i*pri[j]]=1;            if(i%pri[j]==0)break;        }    }}int main(){    init();    LL l,r;    while(~scanf("%lld%lld",&l,&r))    {        int cnt=0;        me(pr,0);        if(l==1) l++;        for(int i=0;i<val;i++)        {            int a=(l-1)/pri[i]+1;            int b=r/pri[i];            for(int j=a;j<=b;j++)            {                if(j>1)                    pr[j*pri[i]-l]=1;            }        }        int p=-1,max_ans=-1,min_ans=INF,x1,y1,x2,y2;        for(int i=0;i<=r-l;i++)//暴力枚举。。        {            if(pr[i]==0)            {                if(p==-1){p=i;continue;}                if(max_ans<i-p){max_ans=i-p;x1=p+l;y1=i+l;}                if(min_ans>i-p){min_ans=i-p;x2=p+l;y2=i+l;}                p=i;            }        }        if(max_ans==-1)cout<<"There are no adjacent primes."<<endl;        else cout<<x2<<","<<y2<<" are closest, "<<x1<<","<<y1<<" are most distant."<<endl;    }}


我的另类想法:我先判l-r上有哪些是素数,然后存起来。最后查找就好了。

#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <time.h>#include <map>#include <algorithm>using namespace std;#define pi acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=1000;const int maxx=1e6+100;const double EPS=1e-7;const int mod=998244353;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template<class T> void Scan(T &x){    static int CH;    while((CH=getchar())<'0'||CH>'9');    for(x=CH-'0';(CH=getchar())>='0'&&CH<='9';x=(x<<3)+(x<<1)+(CH-'0'));}const int s=10;inline LL mul_mod(LL a, LL b, LL m){    LL c = a*b-(LL)((long double)a*b/m+0.5)*m;    return c<0 ? c+m : c;}LL fast_exp(LL a,LL x,LL m){    LL b = 1;    while (x)    {        if (x & 1)            b = mul_mod(b, a, m);        a = mul_mod(a, a, m);        x >>= 1;    }    return b;}bool MR(LL n){    if (!(n&1))        return n == 2;    LL t = 0, u;    for (u = n-1; !(u&1); u >>= 1)        ++t;    for (int i = 0; i < s; ++i)    {        LL a = rand()%(n-2)+2, x = fast_exp(a, u, n);        for (LL j = 0, y; x != 1 && j < t; ++j, x = y)        {            y = mul_mod(x, x, n);            if (y == 1 && x != n-1)                return false;        }        if (x != 1)            return false;    }    return true;}LL pr[maxx];int main(){    srand();    LL l,r;    while(~scanf("%lld%lld",&l,&r))    {        int val=0;        if(l==1) l++;        for(LL i=l;i<=r;i++)        {            if(MR(i))                pr[val++]=i;        }        if(val<2)        {            puts("There are no adjacent primes.");            continue;        }        LL d1=0,d2=INF;        LL maxl,maxr,minl,minr;        for(int i=0;i<val-1;i++)        {            if(pr[i+1]-pr[i]>d1)            {                maxl=pr[i],maxr=pr[i+1];                d1=pr[i+1]-pr[i];            }            if(pr[i+1]-pr[i]<d2)            {                minl=pr[i],minr=pr[i+1];                d2=pr[i+1]-pr[i];            }        }        printf("%lld,%lld are closest, %lld,%lld are most distant.\n",               minl,minr,maxl,maxr);    }}


原创粉丝点击