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.
     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 }
  • 相关阅读:
    关于FPS游戏痕的问题
    移动端输入框获取焦点后,虚拟键盘弹起,把固定的底部也顶起来了
    正则匹配移动端
    js 判断对象是否为空
    jsonp跨域原理解析
    Webstorm的一些常用快捷键
    webstorm创建js文件时自动生成js注释
    帮你彻底搞懂JS中的prototype、__proto__与constructor(图解)
    this指北 (一篇读懂)
    原型链
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7232146.html
Copyright © 2011-2022 走看看