From 311bc9f38588e1e9db6d0861612fd1809f7decdb Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Mon, 23 Aug 2021 16:15:42 +0800 Subject: [PATCH] =?UTF-8?q?868.=20=E7=AD=9B=E8=B4=A8=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7326048/ --- AcWing/868/868.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) 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; }