zoukankan      html  css  js  c++  java
  • poj 2689 Prime Distance(区间筛选素数)

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9944   Accepted: 2677

    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

     
    用long long 才过啊!!!
     
     
     
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include<algorithm>
     5 using namespace std;
     6 typedef long long ll;
     7 const int N=50000; 
     8 int cnt,p[N+10],flag[N+10];
     9 void get_prime()
    10 {
    11     int i,j;
    12     for(i=2;i<N;i++)
    13     {
    14         if(!flag[i])
    15             p[cnt++]=i;;
    16         for(j=0;j<cnt&&p[j]*i<N;j++)
    17         {
    18             flag[i*p[j]]=1;
    19             if(i%p[j]==0)
    20                 break;
    21         }
    22     }
    23 }
    24 
    25 int a[1000010],pp[1000010];
    26 int main()
    27 {
    28     get_prime();
    29     //printf("%d**%d**
    ",p[0],p[1]);
    30     ll l,r,i,j;
    31        while(~scanf("%lld%lld",&l,&r))
    32        {
    33            //if(l>r)swap(l,r);
    34           if(l<2)l=2;
    35           for(i=0;i<=r-l;i++)a[i]=1;
    36           ll sum=r-l+1;//printf("*****
    ");
    37           for(i=0;a[i]<=r&&i<cnt;i++)
    38           for(j=l/p[i]*p[i];j<=r;j+=p[i])
    39           {
    40               if(j>=l&&j/p[i]>1&&a[j-l])
    41                   a[j-l]=0,sum--;
    42           }
    43           
    44           if(sum<2){printf("There are no adjacent primes.
    ");continue;}
    45           
    46           ll cp=0;
    47           for(i=0;i<=r-l;i++)
    48              if(a[i])    pp[cp++]=i+l;
    49         ll max,min,pos1,pos2;
    50         max=min=pp[1]-pp[0];
    51         pos1=pos2=1;
    52         for(i=2;i<cp;i++)
    53         { 
    54             if(max<pp[i]-pp[i-1])
    55             {
    56                 max=pp[i]-pp[i-1];
    57                 pos1=i;
    58             }
    59             if(min>pp[i]-pp[i-1])
    60             {
    61                 min=pp[i]-pp[i-1];
    62                 pos2=i;
    63             }
    64         } 
    65           printf("%d,%d are closest, %d,%d are most distant.
    ",pp[pos2-1],pp[pos2],pp[pos1-1],pp[pos1])    ;
    66      }
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    springcloud-Netflix创建服务消费者
    Spring Cloud Zuul
    Spring Cloud 熔断器
    树莓派连接启动SSH
    win10红警黑屏和无法打开的处理
    创建索引和主键
    SQL语句增加字段、修改字段、修改类型、修改默认值
    SQL Server 事务隔离级别详解
    SQL Server数据库锁机制及类型
    SQL Server中的锁类型及用法
  • 原文地址:https://www.cnblogs.com/skykill/p/3236526.html
Copyright © 2011-2022 走看看