素数筛选(2次):poj2689 Prime Distance

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.
解题思路:

首先,不同于普通素数筛选,由于上限U很大,只进行一次从2-2147483647的筛选绝对会超时,跟不行不通,除非改进算法,否则就要换一下思路。

然后,可以考虑只对L-U内的素数进行筛选,筛选的方法是从1-sqrt(2147483647)内的素数筛选(素因子肯定不超过sqrt(2147483647)),getprime()函数实现。

接着,用可能的素因子去筛选L-U区间的数,和筛法的思想相同(但要注意从哪开始,begin怎么确定),将所有的素数存到数组q[]中。

最后,for循环一次,找到相邻的q[i+1]、q[i]的最大值和最小值,输出。

思路并不难,最蹩脚的是数据的溢出问题。

①for(int i=l;i<=u;i++) 当i=u=2147483647时,i++会溢出,进入死循环,所以要处理一下。

②long long begin=l%p[i]?l+(p[i]-l%p[i]):l+l%p[i];在确定begin时,有加法可能会溢出,可以用long long p[]和long long begin,也可以都换成long long或unsigned int

参考代码和部分注释:

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
#include 
       
       
         #include 
        
          #include 
         
           using namespace std; const int maxn=50000+10; int l,u,k,q[maxn]; bool prime1[maxn],prime2[1000010]; long long p[2*maxn]; void getprime() //1-50000的素数筛选 { memset(prime1,true,sizeof(prime1)); prime1[1]=0; k=0; for(int i=2;i<=maxn;i++){ if(prime1[i]){ p[k++]=i; for(int j=i+i;j<=maxn;j+=i) prime1[j]=false; } } } void solve() { memset(prime2,true,sizeof(prime2));if(l==1) l++; //素数筛选问题对1的处理常是个坑................... for(int i=0;i 
          
            max) {max=q[i+1]-q[i];max1=q[i];max2=q[i+1];} cout< 
           
             <<","< 
            
              <<" are closest, "< 
             
               <<","< 
              
                <<" are most distant."< 
               
                 >l>>u){ solve(); } return 0; } 
                
               
              
             
            
           
          
         
       
     
     
    
    
   
   




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值