您的位置:首页 > Web前端

2013杭电warm_up1 1010 Difference Between Primes

2013-09-19 16:45 405 查看
题目地址: 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\n",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\n",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\n",start+x,start);
break;
}
}
}

if(fail)  printf("FAIL\n");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: