zoukankan      html  css  js  c++  java
  • poj2689

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    const int maxn=1000000;
    
    long long v[maxn],prime[maxn];
    bool ans[1000000+10];
    long long ans2[1000000+10];
    
    int main(){
        int l,r;
        while(scanf("%d%d",&l,&r)!=EOF){
        memset(ans,0,sizeof(ans));
        memset(ans2,0,sizeof(ans2));
        memset(prime,0,sizeof(prime));
        memset(v,0,sizeof(v));
        int n;
        n=sqrt(r)+100;
        int top=0;
        for (int i=2;i<=n;i++){
            if(v[i]==0){
                prime[++top]=i;
                v[i]=i;
            }
            for (int j=1;j<=top;j++){
                if(prime[j]>v[i]||prime[j]>n/i) break;
                v[i*prime[j]]=prime[j];
            }
        }
        for (int i=1;i<=top;i++){
            for (int j=ceil(double(l)/double(prime[i]))>1?ceil(double(l)/double(prime[i])):2;j*prime[i]<=r;j++){
                ans[prime[i]*j-l]=1;
            }
        }
        int m=0;
        for (int i=0;i<=r-l;i++){
            if(!ans[i]) {
                    ans2[++m]=i;
            }
        }
        if(m<=1) {
                printf("There are no adjacent primes.
    ");
                continue;
        }
    
        long long maxl,maxr,maxans=0;
        long long minl,minr,minans=10000000;
        for (int i=1;i<m;i++){
            if(ans2[i+1]-ans2[i]>=maxans){
                maxl=ans2[i];
                maxr=ans2[i+1];
                maxans=ans2[i+1]-ans2[i];
            }
            if(ans2[i+1]-ans2[i]<=minans){
                minl=ans2[i];
                minr=ans2[i+1];
                minans=ans2[i+1]-ans2[i];
            }
        }
        printf("%lld,%lld are closest, %lld,%lld are most distant.
    ",minl+l,minr+l,maxl+l,maxr+l);
     }
    return 0;
    }
  • 相关阅读:
    L1-009. N个数求和
    L1-008. 求整数段和
    L1-007. 念数字
    L1-006. 连续因子
    L1-005. 考试座位号
    L1-003. 个位数统计
    mtk preloader学习笔记
    android kernel启动学习笔记
    android MTK平台编译UBOOT学习笔记
    dota2输入法无候选框?
  • 原文地址:https://www.cnblogs.com/lmjer/p/9010548.html
Copyright © 2011-2022 走看看