src2020/lg3383_no.sushu/main.cpp

35 lines
716 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include<cstdio>
using namespace std;
bool pan[100000001] = { 0 };
int chu[1000001] = { 0 }, now = 0;
//线性筛
void findsu(int n){
for (int i = 2; i <= n; i++) {
if (!pan[i]) {
chu[++now] = i;
}
for (int j = 1; j <= now && i * chu[j] <= n; j++) {
pan[i * chu[j]] = 1;
//重要确保用最大的因数来乘如用6*2=12而避免4*3=12重复计算
if (i % chu[j] == 0) {
break;
}
}
}
}
int main()
{
int n, e;
scanf("%d%d",&n,&e);
findsu(n);//筛出素数
for (int i = 1; i <= e; i++) {
scanf("%d",&n);
printf("%d\n",chu[n]);
}
}