• 二次筛选区间素数 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.
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <cmath>
     5 using namespace std;
     6 const int N = 5e5;
     7 const int M = 1e6+10;
     8 int pre[N], f[M], vis[N], t;
     9 void getPri() {
    10     for(int i = 2; i <= N; i ++) {
    11         if(!vis[i]) {
    12             pre[++t] = i;
    13             for(int j = i+i; j <= N; j += i)
    14                 vis[j] = 1;
    15         }
    16     }
    17 }
    18 int main() {
    19     getPri();
    20     int L, U;
    21     while(scanf("%d%d",&L,&U)!=EOF) {
    22         if(L < 2) L = 2;
    23         memset(f,0,sizeof(f));
    24         for(int i = 1; i <= t; i ++) {
    25             int s = L/pre[i], e = U/pre[i];
    26             if(L%pre[i]) s++;
    27             if(s < 2) s = 2;
    28             for(int j = s; j <= e; j ++)
    29                 f[j*pre[i]-L] = 1;
    30         }
    31         int p = -1, Max = -1, Min = 1000000000, x1, x2, y1, y2;
    32         for(int i = 0; i <= U-L; i ++) {
    33             if(!f[i]){
    34                 if(p == -1){
    35                     p = i;continue;
    36                 }
    37                 if(i-p > Max){
    38                     Max = i-p; x1 = p+L; y1 = i+L;
    39                 }
    40                 if(i-p < Min) {
    41                     Min = i-p; x2 = p+L; y2 = i+L;
    42                 }
    43                 p = i;
    44             }
    45         }
    46         if(Max == -1) printf("There are no adjacent primes.
    ");
    47         else printf("%d,%d are closest, %d,%d are most distant.
    ",x2,y2,x1,y1);
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    【JZOJ4928】【NOIP2017提高组模拟12.18】A
    【JZOJ4922】【NOIP2017提高组模拟12.17】环
    【JZOJ4923】【NOIP2017提高组模拟12.17】巧克力狂欢
    【JZOJ4924】【NOIP2017提高组模拟12.17】向再见说再见
    【JZOJ4919】【NOIP2017提高组模拟12.10】神炎皇
    【JZOJ4920】【NOIP2017提高组模拟12.10】降雷皇
    【JZOJ4921】【NOIP2017提高组模拟12.10】幻魔皇
    【罗宾欺诈者】回环符文——回文树(回文自动机)
    【怪物】KMP畸形变种——扩展KMP
    【51NOD1304】字符串的相似度
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7232146.html
Copyright © 2020-2023  润新知