Background
本题已更新,从判断素数改为了查询第 �k 小的素数
提示:如果你使用 cin
来读入,建议使用 std::ios::sync_with_stdio(0)
来加速。
Description
如题,给定一个范围 �n,有 �q 个询问,每次输出第 �k 小的素数。
Input
第一行包含两个正整数 �,�n,q,分别表示查询的范围和查询的个数。
接下来 �q 行每行一个正整数 �k,表示查询第 �k 小的素数。
Output
输出 �q 行,每行一个正整数表示答案。
Sample 1
Inputcopy | Outputcopy |
---|---|
100 5 1 2 3 4 5 | 2 3 5 7 11 |
Hint
【数据范围】
对于 100%100% 的数据,�=108n=108,1≤�≤1061≤q≤106,保证查询的素数不大于 �n。
Data by NaCly_Fish.
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<deque>
#include<map>
#define endl '\n'
#define ll long long
//#define int ll
using namespace std;
const int N = 1e8 + 7;
deque<int> d;
map<int, int> mp;
bool a[N];
int p[N];
signed main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, q;
cin >> n >> q;
int cnt = 0;
// isprime();
memset(a, 1, sizeof(a));
a[1] = 0;
for (int i = 2;i <= n;i++)
{
if (a[i]) p[++cnt] = i;
for (int j = 1;j <= cnt && (ll)p[j] * i <= n;j++)
{
a[p[j] * i] = 0;
if (!i % p[j]) break;
}
}
while (q--)
{
int k;
cin >> k;
cout << p[k] << endl;
}
return 0;
}