zoukankan      html  css  js  c++  java
  • POJ2689 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.
    

    Source

     
    题解:
     
    由于L和U太大,直接求解会爆掉
    注意到int范围内的合数,其最小质因子不会超过sqrt(int),即不超过50000
    考虑先找出50000以内的素数,再用这些素数去筛掉[L,U]内的合数,然后统计即可
    另外注意1
     
    代码如下:
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #define MAXN 50000
    using namespace std;
    int L,U,cnt=0,tot=0,a1,b1,a2,b2,lg=0,sg=1e9;
    int prime1[MAXN],prime2[1000005];
    bool check[MAXN],mark[1000005];
    void get_prime()
    {
        for(int i=2;i<=MAXN;i++)
            {
                if(check[i])
                   prime1[++cnt]=i;
                for(int j=1;j<=cnt&&prime1[j]*i<=MAXN;j++)
                    {
                        check[i*prime1[j]]=false;
                        if(i%prime1[j]==0)
                           break;
                    }   
            }
    }
    int main()
    {
        memset(check,true,sizeof(check));
        get_prime();
        while(scanf("%d%d",&L,&U)!=EOF)
              {
                    memset(mark,true,sizeof(mark));
                    if(L==1)
                       mark[1]=false;
                    for(int i=1;i<=cnt;i++)
                        {
                              if(prime1[i]>U)
                                 break;
                              int l=L/prime1[i]>=2?L/prime1[i]:2,r=U/prime1[i];
                              for(int j=l;j<=r;j++)
                                  mark[prime1[i]*j-L+1]=false;
                        }
                  for(int i=1;i<=U-L+1;i++)
                      {
                          if(mark[i])
                             prime2[++tot]=i; 
                      }          
                  for(int i=2;i<=tot;i++)
                      {
                            int dis=prime2[i]-prime2[i-1];
                            if(dis>lg)
                               {
                                   lg=dis;
                                   a1=prime2[i-1]+L-1;
                                   b1=prime2[i]+L-1;
                               }
                            if(dis<sg)
                             {
                                  sg=dis;
                                  a2=prime2[i-1]+L-1;
                                  b2=prime2[i]+L-1;
                             }   
                      }
                  if(tot<2)
                     printf("There are no adjacent primes.
    ");    
                  else    
                     printf("%d,%d are closest, %d,%d are most distant.
    ",a2,b2,a1,b1);
                  tot=0,lg=0,sg=1e9;               
              }   
        return 0;
    }
    View Code
  • 相关阅读:
    Logistic 与 softmax
    opencv::KMeans图像分割
    opencv::KMeans方法概述
    opencv::opencv_traincascade.exe
    opencv::opencv_createsamples.exe
    opencv::视频人脸检测
    opencv实践::对象提取与测量
    opencv实践::透视变换
    opencv实践::对象计数
    opencv实践::对象的提取
  • 原文地址:https://www.cnblogs.com/nanjolno/p/9306297.html
Copyright © 2011-2022 走看看