zoukankan      html  css  js  c++  java
  • poj-2689-素数区间筛

    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

        给出[L,R],求区间内的素数,R<=2147483647,R-L<=1000000, 注意到只用sqrt(R)以内的素数就可以筛出[L,R]里面的素数,
    可以先对sqrt(MAX_INT)内的素数打一个表。对于[L,R]的询问,用小于等于sqrt(R)的素数筛一下然后统计一下就好了。注意L<2的时候
    要特判一下否则容易把1也给打进去。
        
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<vector>
     6 using namespace std;
     7 #define LL long long 
     8 #define mp make_pair
     9 #define pb push_back
    10 #define inf 0x3f3f3f3f
    11 int maxn=100005;
    12 vector<int>prime;
    13 vector<int>p;
    14 bool is[1000100];
    15 void init(){
    16     is[0]=is[1]=1;
    17     for(LL i=2;i<=maxn;++i){
    18         if(!is[i]) prime.push_back(i);
    19         for(LL j=0;j<prime.size()&&i*prime[j]<=maxn;j++){
    20             is[i*prime[j]]=1;
    21             if(i%prime[j]) break;
    22         }
    23     }
    24 }
    25 void solve(LL L,LL R){
    26     p.clear();
    27     memset(is,0,sizeof(is));
    28     for(LL i=0;i<prime.size()&&1LL*prime[i]*prime[i]<=R;i++){
    29         LL s=L/prime[i]+(L%prime[i]>0);
    30         if(s==1)s=2;
    31         for(LL j=s;j*prime[i]<=R;j++){
    32             if(j*prime[i]>=L) is[j*prime[i]-L]=1;
    33         }
    34     }
    35     for(int i=0;i<=R-L;i++){
    36         if(!is[i]&&i+L>=2) p.push_back(i+L);
    37     }
    38 }
    39 int main(){
    40     LL L,R;
    41     init();
    42     while(scanf("%lld%lld",&L,&R)!=EOF){
    43         solve(L,R);
    44     
    45         if(p.size()<2) puts("There are no adjacent primes.");
    46         else{
    47             int c1,c2,m1,m2;
    48             c1=m1=p[0];
    49             c2=m2=p[1];
    50             for(int i=1;i<p.size();++i){
    51                 if(p[i]-p[i-1]<c2-c1){
    52                     c1=p[i-1];
    53                     c2=p[i];
    54                 }
    55                 if(p[i]-p[i-1]>m2-m1){
    56                     m1=p[i-1];
    57                     m2=p[i];
    58                 }
    59             }
    60             printf("%d,%d are closest, %d,%d are most distant.
    ",c1,c2,m1,m2);
    61         }
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    (uC/OS-II学习笔记) 事件标志……
    (uC/OS-II学习笔记) 消息邮箱&&消息队列
    (uC/OS-II学习笔记)关于共享资源与信号量
    (uC/OS-II学习笔记)uC/OS-II 时间管理
    (uC/OS-II学习笔记)uC/OS-II在kinetis K60上移植与任务建立
    (Kinetis K60)RTC实时时钟
    (Kinetis K60)flash读写
    JQuery官方学习资料(译):避免与其他库的冲突
    JQuery官方学习资料(译):$( document ).ready()
    JQuery官方学习资料(译):$ vs $()
  • 原文地址:https://www.cnblogs.com/zzqc/p/9480469.html
Copyright © 2011-2022 走看看