题目连接:https://codeforc.es/contest/1474/problem/B
Positive integer x is called divisor of positive integer y, if y is divisible by x without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.
We gave you an integer d and asked you to find the smallest positive integer a, such that
a has at least 4 divisors;
difference between any two divisors of a is at least d.
Input
The first line contains a single integer t (1≤t≤3000) — the number of test cases.
The first line of each test case contains a single integer d (1≤d≤10000).
Output
For each test case print one integer a — the answer for this test case.
Example
input
2
1
2
output
6
15
分析
至少四个因子,很明显,只需要找到比 1 + d 大的最小素数,然后再找到这个素数的下一个素数(两者相距要大于 d),两者的乘积就是答案。
可以用二分来找。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 1000007
ll t,d;
ll prime[maxn];
ll visit[maxn];
void Prime(){
for (ll i = 2;i <= maxn; i++) {
if (!visit[i]) {
prime[++prime[0]] = i;
}
for (ll j = 1; j <= prime[0] && i*prime[j] <= maxn; j++) {
visit[i*prime[j]] = 1;
if (i % prime[j] == 0) {
break;
}
}
}
}
int main()
{
Prime();
//cout<<prime[78499]<<endl;
scanf("%lld",&t);
while(t--)
{
scanf("%lld",&d);
ll tmp = 1 + d;
int pos = lower_bound(prime + 1, prime + 1 + prime[0], tmp) - prime;
ll tmp1 = prime[pos];
pos = upper_bound(prime + 1, prime + 1 + prime[0], tmp1) - prime;
while(prime[pos] - tmp1 < d) pos++;
printf("%lld\n",prime[pos] * tmp1);
}
return 0;
}