zoukankan      html  css  js  c++  java
  • poj 2689 Prime Distance

    Prime Distance

    Time Limit: 1000MS  Memory Limit: 65536K

    Total Submissions: 29401  Accepted: 7556

    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.

    题意:在区间[L,U]中找出一对距离最近的相邻素数和一对距离最远的相邻素数,距离相同时,取第一对。

    题解:区间素数筛。由于数据范围太大,无法用数组维护素数。用区间[1,]的素数去更新区间[L,U]的素数。(用已知小范围素数推未知大范围素数)。区间间差不超多1000000,利用下标偏移,区间[0,U-L]维护区间[L,U]

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    const int inf=0x3f3f3f3f;
    int prime[5000],top;
    bool flag[50005]={1,1},isp[1000005];
    void init();
    void solve(int L,int R);
    int main()
    {
      init();
      int L,R,l1,r1,l2,r2,i,j,mmin,mmax;
      while(~scanf("%d%d",&L,&R))
      {
        if(L==1) L=2; //重要
        {
          fill(isp,isp+1000005,1);
          mmin=inf;mmax=-inf;
        }
        solve(L,R);
        for(i=0;i<=R-L;i++)
        {
          if(!isp[i]) continue;
          for(j=i+1;j<=R-L;j++)
           if(isp[j]) 
            {
              if(mmax<(j-i)) mmax=j-i,l1=i+L,r1=j+L;
              if(mmin>(j-i)) mmin=j-i,l2=i+L,r2=j+L;
              break;
            }
        }
        if(mmax==-inf) printf("There are no adjacent primes.
    ");
        else printf("%d,%d are closest, %d,%d are most distant.
    ",l2,r2,l1,r1);
      }
      return 0;
    }
    void init()
    {
      int i,j;
      for(i=2;i<50005;i++)
      {
       if(!flag[i]) prime[top++]=i;
       for(j=0;j<top&&i*prime[j]<50005;j++)
       {
         flag[i*prime[j]]=1;
         if(i%prime[j]==0) break;
       }
      }
    }
    void solve(int L,int R)
    {
      long long i,l;
      for(i=2;i*i<=R;i++)
      {
        if(flag[i]) continue;
        for(l=max(2LL,((i+L-1)/i))*i;l<=R;l+=i) isp[l-L]=0;
      }
    }
    本博客仅为本人学习,总结,归纳,交流所用,若文章中存在错误或有不当之处,十分抱歉,劳烦指出,不胜感激!!!
  • 相关阅读:
    shell脚本编程-结构化命令3-while、until命令
    shell脚本编程-结构化命令2-for命令
    sscanf解析复杂字符串,双引号通配符的使用问题
    shell脚本编程-结构化命令1-分支语句
    shell脚本编程基础
    linux系统管理的基本命令2
    linux系统管理的基本命令
    redis
    Eclipse启动报错
    java斗地主发牌源码
  • 原文地址:https://www.cnblogs.com/VividBinGo/p/11390629.html
Copyright © 2011-2022 走看看