文章
68
粉丝
691
获赞
26
访问
578.4k
埃及筛法找到所有范围内的素数,然后直接lowerbound即可,
#define ll int
#define MAX 20005
#define vec vector<ll>
int main() {
bool isP[MAX]; vec primes;
fill(isP, isP + MAX, 1);
for (int i = 2; i < MAX; i++) {
if (isP[i])
for (int j = i * 2; j < MAX; j += i)
isP[j] = 0;
}
for (int i = 2; i < MAX; i++)if (isP[i])primes.push_back(i);
ll n;
while (cin >> n) {
ll id = lower_bound(primes.begin(), primes.end(), n) - primes.begin();
cout << primes[id] << endl;
}
}
登录后发布评论
暂无评论,来抢沙发