8.J - 线性筛素数

Background

本题已更新,从判断素数改为了查询第 �k 小的素数
提示:如果你使用 cin 来读入,建议使用 std::ios::sync_with_stdio(0) 来加速。

Description

如题,给定一个范围 �n,有 �q 个询问,每次输出第 �k 小的素数。

Input

第一行包含两个正整数 �,�n,q,分别表示查询的范围和查询的个数。

接下来 �q 行每行一个正整数 �k,表示查询第 �k 小的素数。

Output

输出 �q 行,每行一个正整数表示答案。

Sample 1

InputcopyOutputcopy
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值