Description
We call a number is a sphenic number if it is a product of 3 distinct primes. The smallest 10 sphenic numbers are 30, 42, 66, 70, 78, 102, 105, 110, 114, 130. Now you need to find the Kth smallest sphenic number.
Input
There is an integer T (1 <= T <= 10^5) in the first line, indicates there are T test cases in total.
Each test case contains one line with an integer K (1 <= K <= 10^5), indicates you need to find the Kth smallest sphenic number.
Output
For each test case, you should print the Kth smallest sphenic number in one line.
Sample Input
2110
Sample Output
30130
思路:暴力,先用素数筛选法,然后暴力组合,然后排序。注意范围, 不断缩小范围。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long ans[1500005];
long long p[200005], a[200005], tt = 0;
void prime()
{
int i, n=130000, j;
a[1] = 0;
for(i = 2; i <= n; i++)
a[i] = 1;
for(i = 2; i <= n; i++)
if(a[i])
{
p[tt++] = i;
for(j = i; j <= n;j += i)
a[j] = 0;
}
}
int main()
{
prime();
int T, n, t = 0;
for(int i = 0; i < tt; i++)
{
for(int j = i + 1; j < tt; j++)
{
for(int k = j + 1; k < tt; k++)
{
if(p[i] * p[j] * p[k]>539251)
break;
ans[t++] = p[i] * p[j] * p[k];
}
}
}
sort(ans, ans + t);
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
printf("%lld\n", ans[n - 1]);
}
return 0;
}