fork(3) download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. // NOTE: gP(n) is incorrect for even values of n
  7. #define N 1000000
  8. unsigned int mark[N / 64];
  9. #define gP(n) (mark[n>>6]&(1<<((n>>1)&31)))
  10. #define rP(n) (mark[n>>6]&=~(1<<((n>>1)&31)))
  11.  
  12. int cnt[N + 1];
  13.  
  14. void sieve() {
  15. memset( mark, -1, sizeof( mark ) );
  16. unsigned int i;
  17. unsigned int sqrtN = ( unsigned int )sqrt( ( double )N ) + 1;
  18.  
  19. for( i = 3; i < sqrtN; i += 2 ) if( gP( i ) ) {
  20. unsigned int i2 = i + i;
  21. for( unsigned int j = i * i; j < N; j += i2 ) rP( j );
  22. }
  23. cnt[1] = 1;
  24. cnt[2] = 1;
  25. for( i = 3; i < N; i += 2)
  26. if (gP(i)) cnt[i] = 1;
  27.  
  28. for(int i=1; i <= N; ++i) cnt[i] += cnt[i-1];
  29. }
  30.  
  31. int main() {
  32. ios :: sync_with_stdio(false); cin.tie(NULL);
  33. sieve();
  34. int ntest; cin >> ntest;
  35. for(int test = 1; test <= ntest; ++test) {
  36. int n; cin >> n;
  37. cout << "Case #" << test << ": " << cnt[n] << endl;
  38. }
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0.01s 7444KB
stdin
1
5
stdout
Case #1: 4