hdu 4715 Difference Between Primes

来源:互联网 发布:软件打不开 编辑:程序博客网 时间:2024/05/22 10:50

http://acm.hdu.edu.cn/showproblem.php?pid=4715

#include<iostream>#include<stdio.h>#include <cmath>#include <algorithm>#include <string>#include <cstring>using namespace std;#define N 1000000bool isprm[N];long long xx[N];void isprime(){    int i,j,k=0;    int s,e=sqrt( double(N) )+1;    //sqrt是对于double数开平方    memset(isprm,1,sizeof(isprm));    isprm[0] = isprm[1] = 0;        for(i=4 ; i < N; i=2+i)        isprm[i]=0;            for(i=3; i<e; i=2+i)        if(isprm[i])            for(s=i*2,j=i*i; j<N; j=j+s)                isprm[j]=0;                        //因为j是奇数,所以+奇数后是偶数,不必处理}int main(){    int coun=0;    int i;    isprime();    for(i=0; i<N; i++)        if(isprm[i])        {            xx[coun++]=i;        }    int T;    long long t;    long long t2;    bool flag;    scanf("%d",&T);    while(T--)    {        scanf("%lld",&t);        flag=false;        for(i=0; i<coun; ++i)        {            t2=t+xx[i];            if(isprm[t2])            {                flag=true;                break;            }        }        if(flag)        {            printf("%lld %lld\n",t2,xx[i]);        }        else        {            printf("FAIL\n");        }    }    return 0;}

0 0
原创粉丝点击