hdu 4715 Difference Between Primes

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 1000000
bool 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;
}

你可能感兴趣的:(hdu 4715 Difference Between Primes)