2013杭电warm_up1 1010 Difference Between Primes

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4715

首先打素数表,然后分三类  x=0,>0, <0就ac了

嗯嗯 验证了那句话 "这个问题我没解决,但是ac还是没问题的"  暴力能过的背后,如果数据不水,那么一定藏着数学原理保证实际运行效率不低。


代码:

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;

#define  N 10000000
bool p[N+1];


void pre()
{
  int d=sqrt(N);
   for(int i=2;i<=d;i++)
      if(p[i]==0)
      for(int j=i*i;j<=N;j+=i)
         p[j]=1;

  p[0]=1;
  p[1]=1;

}
int main()
{
     pre();

     int n,x;
     cin>>n;

     for(int i=0;i<n;i++)
     {
        int start=2;
        bool fail=true;
        scanf("%d",&x);
        if(x==0)
        {
          fail=0;
         printf("%d %d
",start+x,start);
        }
        else if(x>0)
        {
            for(start=2;start<=10000000-x;start++)
            {
               if(!p[start]&&!p[start+x])
               {
                  fail=false;
                  printf("%d %d
",start+x,start);
                  break;
               }
            }
        }
        else if(x<0)
        {
            for(start=-x;start<10000000;start++)
            {
               if(!p[start]&&!p[start+x])
               {
                  fail=false;
                  printf("%d %d
",start+x,start);
                  break;
               }
            }
        }



        if(fail)  printf("FAIL
");


     }
}



原文地址:https://www.cnblogs.com/814jingqi/p/3339257.html