B. Different Divisors
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Positive integer 𝑥 is called divisor of positive integer 𝑦, if 𝑦 is divisible by 𝑥 without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.
We gave you an integer 𝑑 and asked you to find the smallest positive integer 𝑎, such that
𝑎 has at least 4 divisors;
difference between any two divisors of 𝑎 is at least 𝑑.
Input
The first line contains a single integer 𝑡 (1≤𝑡≤3000) — the number of test cases.
The first line of each test case contains a single integer 𝑑 (1≤𝑑≤10000).
Output
For each test case print one integer 𝑎 — the answer for this test case.
Example
input
2
1
2
output
6
15
Note
In the first test case, integer 6 have following divisors: [1,2,3,6]. There are 4 of them and the difference between any two of them is at least 1. There is no smaller integer with at least 4 divisors.
In the second test case, integer 15 have following divisors: [1,3,5,15]. There are 4 of them and the difference between any two of them is at least 2.
The answer 12 is INVALID because divisors are [1,2,3,4,6,12]. And the difference between, for example, divisors 2 and 3 is less than 𝑑=2.
解题思路:
1、筛法求素数
2、ans只用考虑四个因子的情况,其中一对因子必为1和它自身,为保证ans最小且每个因子之间相差>=d,则另一对因子必为质数。
代码
int main(){
int t;
cin >> t;
int max=1000000;
bool a[max];
for(int i=0;i<=max;i++) a[i]=1;
a[0]=0; a[1]=0;
for(int i=2;i<=max;i++){
if(a[i]){
for(int j=i*2;j<=max;j+=i) a[j]=0;
}
}//注意质数的bool数组放在test循环外层,放内层会TLE
while(t--){
int d,ans=1,num=1,last=1;//last为上一次的因子,num为因子数
cin>>d;
for(int i=1+d;i<=max;i++){
if(i-last>=d && a[i]==1) { ans*=i; last=i; num++; }
if(num>=3) { printf("%d\n",ans); break; }//因为答案可以理解为前三个因子的积,若不包括1,即为中间两个因子的积
}
}
return 0;
}