Cod sursa(job #2048505)

Utilizator CosaMateiMatei Cosa Gabriel CosaMatei Data 26 octombrie 2017 09:19:12
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("ciur.in");
ofstream out("ciur.out");

const int MAXSIZE = 100000000/2/8+1;
char p[MAXSIZE];

int ciur(int n) {
  int i, j, nr = 1;
  for (i = 1; ((i * i) << 1) + (i << 1) <= n; i += 1) {
    if ((p[i >> 3] & (1 << (i & 7))) == 0) {
      for (j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1) {
        p[j >> 3] |= (1 << (j & 7));
      }
    }
  }
  for (i = 1; 2 * i + 1 <= n; ++i)
       if ((p[i >> 3] & (1 << (i & 7))) == 0)
           nr++;
  return nr;
}

int main()
{
    long long n;
    in>>n;
    out<<ciur(n);
    return 0;
}