跳到正文

算法·理论

欧拉筛模板

GaoMingYuan约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年2月12日
更新
2026年2月12日
篇幅
约 1 分钟

版本

  1. v2当前
  2. v1

已 ACAC P3383【模板】线性筛素数 & P5723【深基4.例13】质数口袋

C++
#include<iostream>
using namespace std;
const int N = 1e8 + 7;
bool noP[N];
int ct, pr[N];
void euler(int n){
	noP[0] = noP[1] = true;
	for(int i = 2; i < N; i++){
		if(!noP[i]) pr[++ct] = i;
		for(int j = 1; j <= ct and (long long) i * pr[j] < N; ++j){
			noP[i * pr[j]] = true;
			if(i % pr[j] == 0) break;
		}
	}
}
int main(){
	std::ios::sync_with_stdio(0);
	int n, q;
	cin>>n>>q;
	euler(n);
	for(int i = 0 ; i < q; i++){
		int t;
		cin>>t;
		cout<<pr[t]<<endl;
	}
}
原文