设为首页 加入收藏

TOP

poj2689Prime Distance(数论)
2015-07-20 18:04:08 来源: 作者: 【 】 浏览:2
Tags:poj2689Prime Distance 数论

题目链接

啊哈哈,点我

题目:

Prime Distance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11882 Accepted: 3172

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.

Source

Waterloo local 1998.10.17
思路是:

这道题目是对筛法素数表求一个区间之内的差距最小,最大的素数对。。

首先数据范围是达到了int的上线,所以对素数表的选择到底打多大呢??、

我们是根据小的素数表去筛后面的区间。。

所以v=ri/prime[i]...所以当ri等于int的上线时的时候就是2的31次方。。则最小的素数为2的31次方开方。。。

大约在47000左右。。所以素数表就解决了。

接下来就是筛区间了。。因为由一个不记得的定理的一个合数等于若干素数的乘积。。。所以我们用le,ri分别确定左,右边界。。之后在之间的所有的数都被筛掉了。。。

然后就是因为数据范围在int范围内,所以数组只能开1000000了。。如何避免数组越界呢??

因为0

代码为:


#include
  
   
#include
   
     #include
    
      #include
     
       using namespace std; const int maxm=46341+10; int prime[maxm],vis[maxm],a[1000000+10]; int c=0,m; void pre_judge() { int i,j; memset(vis,0,sizeof(vis)); m=sqrt(maxm+0.5); for(i=2;i<=m;i++) { if(!vis[i]) { for(j=i*i;j<=maxm;j+=i) vis[j]=1; } } for(i=2;i<=maxm;i++) { if(!vis[i]) prime[c++]=i; } } int main() { int min_le,min_ri,max_le,max_ri,min_path,max_path; int le,ri,pd,i,j; pre_judge(); while(~scanf("%d%d",&le,&ri)) { memset(a,0,sizeof(a)); min_path=1000000+10; pd=max_path=-1; if(le==1) le=le+1; for(i=0;i
      
       1) a[j*prime[i]-le]=1; } } for(int i=0;i<=ri-le;i++) { if(!a[i]) { if(pd==-1) { pd=i; continue; } if(i-pd>max_path) { max_le=pd+le; max_ri=i+le; max_path=i-pd; } if(i-pd
       
        

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 1068 Girls and Boys(匈牙利.. 下一篇HDU4734――F(x)(数位DP)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: