【Difference Between Primes HDU - 4715】【素数筛法打表+模拟】
这道题很坑,注意在G++下提交,否则会WA,还有就是a或b中较大的那个数的范围。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1e6 + 10;
int prime[maxn];
bool isprime[maxn];
int init()
{
memset(prime, 0, sizeof(prime));
isprime[0] = isprime[1] = false;
for(int i = 2; i <= maxn; i++)
isprime[i] = true;
int cnt = 0;
for(int i = 2; i <= maxn; i++)
{
if(isprime[i])
{
for(int j = i * 2; j <= maxn; j += i)
isprime[j] = false;
prime[cnt++] = i;
}
}
return cnt;
}
int T, n;
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int cnt = init();
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
int sign = 0, flag = 0;
if(n < 0)
{
n = -n;
sign = 1;
}
for(int i = 0; i < cnt; i++)
{
if(n + prime[i] < maxn - 10 && isprime[n + prime[i]])
{
flag = 1;
if(sign)
printf("%d %d\n", prime[i], n + prime[i]);
else
printf("%d %d\n", prime[i] + n, prime[i]);
break;
}
}
if(!flag)
printf("FAIL\n");
}
}