zoukankan      html  css  js  c++  java
  • POJ 2689

    题目链接:http://poj.org/problem?id=2689

    Time Limit: 1000MS Memory Limit: 65536K

    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.

    题意:

    给出 $st$ 与 $ed$ ($1 le st < ed le 2147483647$ 且 $ed - st le 1e6$),求 $[st,ed]$ 区间内,相邻的两个素数中,差最小的和差最大的(若存在差同样大的一对素数,则有先给出最小的一对素数);

    题解:

    显然,不可能直接去筛 $2147483647$ 以内的素数;

    由于任何一个合数 $n$ 必定包含一个不超过 $sqrt{n}$ 的质因子,

    那么,我们不妨先用欧拉筛法筛出 $[0,46341]$ 区间内的素数($46341 approx sqrt{2147483647}$);

    然后对于每个test case的 $[st,ed]$ 区间,用筛出来的质数再去标记 $[st,ed]$ 内的合数,剩下来的就是质数了。

    AC代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #define pb(x) push_back(x)
    using namespace std;
    typedef long long ll;
    const int maxn=1e6+5;
    vector<ll> p;
    bool vis[maxn];
    
    const int MAX=46350;
    bool noprm[MAX+3];
    vector<int> prm;
    void Erato()
    {
        noprm[0]=noprm[1]=1;
        for(int i=2;i<=MAX;i++)
        {
            if(noprm[i]) continue;
            prm.pb(i);
            for(int j=i;j<=MAX/i;j++) noprm[i*j]=1;
        }
    }
    
    int main()
    {
        Erato();
    
        ll L,R;
        while(cin>>L>>R)
        {
            for(ll i=L;i<=R;i++) vis[i-L]=0;
            for(int i=0;i<prm.size();i++)
            {
                ll st=max(2LL,L/prm[i]+(L%prm[i]>0)), ed=R/prm[i];
                for(ll k=st;k<=ed;k++) vis[k*prm[i]-L]=1;
            }
            if(L==1) vis[0]=1;
    
            p.clear();
            for(ll i=L;i<=R;i++) if(!vis[i-L]) p.pb(i);
            if(p.size()<=1) cout<<"There are no adjacent primes.
    ";
            else
            {
                int mn=1, mx=1;
                for(int i=1;i<p.size();i++)
                {
                    if(p[i]-p[i-1]<p[mn]-p[mn-1]) mn=i;
                    if(p[i]-p[i-1]>p[mx]-p[mx-1]) mx=i;
                }
                printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.
    ",p[mn-1],p[mn],p[mx-1],p[mx]);
            }
        }
    }
  • 相关阅读:
    云server之间实时文件同步和文件备份的最简单高效的免费方案
    小程序 通过栈的方式把,上个返回过的页面数据传到当前页面
    mpvue 实例
    mpvue配合less
    java随笔5 完整路径的应用
    java随笔4 java中接参整形转字符串
    java随笔2 变量类定义
    java随笔1 常用快捷键 补全和补全提示等
    对象(针对对象)
    事务的2种简单实例
  • 原文地址:https://www.cnblogs.com/dilthey/p/7577275.html
Copyright © 2011-2022 走看看