Cod sursa(job #1692945)

Utilizator msciSergiu Marin msci Data 21 aprilie 2016 23:32:41
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;

const int N = 100000000;

char s[N] = {0};

int main() {
  freopen("ciur.in", "r", stdin);
  freopen("ciur.out", "w", stdout);
  int n;
  scanf("%d", &n);
  //s[i >> 3] & (1 << (i & 7)) == 0 if 2 * i + 1 is prime
  for (int i = 1; ((i * i) << 1) + (i << 1) <= n; i++) {
    if ((s[i >> 3] & (1 << (i & 7))) == 0) {
      for (int j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1)
        s[j >> 3] |= (1 << (j & 7));
    }
  }
  int ans = 1;
  for (int i = 1; (i << 1) + 1 <= n; i++)
    if ((s[i >> 3] & (1 << (i & 7))) == 0) ans++;
  printf("%d\n", ans);
  return 0;
}