mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 03:11:58 +00:00
868. 筛质数
https://www.acwing.com/problem/content/submission/code_detail/7326048/
This commit is contained in:
parent
5e13bb1da0
commit
311bc9f385
@ -2,25 +2,20 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool isprime[1000005];
|
||||
int n, ans;
|
||||
int n, p, primes[1000005];
|
||||
bool not_prime[1000005];
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
for (int i = 0; i <= n; i++) {
|
||||
isprime[i] = true;
|
||||
}
|
||||
isprime[0] = isprime[1] = false;
|
||||
for (int i = 2; i <= n; i++) {
|
||||
if (isprime[i] && 1ll * i * i <= n) {
|
||||
for (int j = i * i; j <= n; j += i) {
|
||||
isprime[j] = false;
|
||||
}
|
||||
if (!not_prime[i]) {
|
||||
primes[p++] = i;
|
||||
}
|
||||
for (int j = 0; primes[j] * i <= n; j++) {
|
||||
not_prime[primes[j] * i] = true;
|
||||
if (i % primes[j] == 0) break;
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= n; i++) {
|
||||
ans += isprime[i];
|
||||
}
|
||||
cout << ans;
|
||||
cout << p << endl;
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user