diff --git a/AcWing/868/868.cpp b/AcWing/868/868.cpp index feb9698f..6258c0cc 100644 --- a/AcWing/868/868.cpp +++ b/AcWing/868/868.cpp @@ -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; }