-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime.cpp
More file actions
50 lines (44 loc) · 1.09 KB
/
prime.cpp
File metadata and controls
50 lines (44 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
const int SQRT_MAXN = 100000 ; //root of the maximum value of N
const int S = 10000 ;
bool nprime [ SQRT_MAXN ] , bl [ S ] ;
int primes [ SQRT_MAXN ] , cnt ;
int n,res;
int calprime(int n)
{
int nsqrt = ( int ) sqrt ( n + .0 ) ;
for ( int i = 2 ; i <= nsqrt ; ++ i )
if ( ! nprime [ i ] ) {
primes [ cnt ++ ] = i ;
if ( i * 1ll * i <= nsqrt )
for (int j = i * i ; j <= nsqrt ; j += i )
nprime [ j ] = true ;
}
int result = 0 ;
for ( int k = 0 , maxk = n / S ; k <= maxk ; ++ k ) {
memset ( bl, 0 , sizeof bl ) ;
int start = k * S ;
for ( int i = 0 ; i < cnt ; ++ i ) {
int start_idx = ( start + primes [ i ] - 1 ) / primes [ i ] ;
int j = max ( start_idx, 2 ) * primes [ i ] - start ;
for ( ; j < S ; j += primes [ i ] )
bl [ j ] = true ;
}
if ( k == 0 )
bl [ 0 ] = bl [ 1 ] = true ;
for ( int i = 0 ; i < S && start + i <= n ; ++ i )
if ( ! bl [ i ] )
++ result ;
}
return result;
}
int main ( )
{
cin >> n ;
res = calprime(n);
cout << res<<endl;
}