Cod sursa(job #3239880)

Utilizator ChopinFLazar Alexandru ChopinF Data 8 august 2024 15:13:26
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>
#include <vector>
using namespace std;
std::string file = "ciur";
std::ifstream fin(file + ".in");
std::ofstream fout(file + ".out");
// #define fin std::cin
// #define fout std::cout
int n, ans;
std::vector<bool> sieve;
void ciur(int n) {
  sieve[0] = sieve[1] = true;
  for (int j = 4; j <= n; j += 2)
    sieve[j] = true;
  for (int i = 3; i * i <= n; i += 2) {
    if (!sieve[i]) {
      for (int j = i * i; j <= n; j += 2 * i) {
        sieve[j] = true;
      }
    }
  }

  if (n < 2) {
    ans = 0;
    return;
  }
  if (n == 2) {
    ans = 1;
    return;
  }
  ans = 1;
  for (int i = 3; i <= n; ++i) {
    if (!sieve[i])
      ans++;
  }
}
int32_t main(int32_t argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  fin.tie(0);
  fout.tie(0);
  fin >> n;
  sieve.resize(n + 1, false);
  ciur(n);
  fout << ans << "\n";
  return 0;
}