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<vector>
#define endl '\n'
#define buff ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
using namespace std;
const int N=1e8+7;
int d[N];
vector<int> prime;
int main()
{
ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
for(int i=2;i<N;i++)
{
if(d[i]==0)
{
d[i]=i;
prime.push_back(i);
}
for(int j=0;j<(int)prime.size();j++)
{
if(prime[j]>d[i]||prime[j]*i>N) break;
d[prime[j]*i]=prime[j];
}
}
int n,q;
cin>>n>>q;
while(q--)
{
int a;
cin>>a;
cout<<prime[a-1]<<'\n';
}
}