File tree Expand file tree Collapse file tree 1 file changed +26
-6
lines changed Expand file tree Collapse file tree 1 file changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,26 @@ using namespace std;
4
4
5
5
using ll = long long ;
6
6
7
+ ll isqrt (ll x) {
8
+ ll l = 0 , r = 1e9 ;
9
+ while (r-l > 1 ) {
10
+ ll m = (l+r)/2 ;
11
+ if (m*m <= x) l = m;
12
+ else r = m;
13
+ }
14
+ return l;
15
+ }
16
+
17
+ ll icbrt (ll x) {
18
+ ll l = 0 , r = 1e6 ;
19
+ while (r-l > 1 ) {
20
+ ll m = (l+r)/2 ;
21
+ if (m*m*m <= x) l = m;
22
+ else r = m;
23
+ }
24
+ return l;
25
+ }
26
+
7
27
vector<int > sieve (int n) {
8
28
vector<bool > b (n+1 );
9
29
for (int i = 3 ; i*i <= n; i += 2 ) {
@@ -22,7 +42,8 @@ vector<int> sieve(int n) {
22
42
}
23
43
24
44
// at least sqrt(n) for pi(n)
25
- int const sqrt_limit = 1e7 + 1 ;
45
+ // bigger values may be faster
46
+ int const sqrt_limit = isqrt(1e12 ) + 1 ;
26
47
27
48
auto primes = sieve(sqrt_limit);
28
49
@@ -52,15 +73,15 @@ ll pi(ll x) {
52
73
return upper_bound (primes.begin (), primes.end (), x) - primes.begin ();
53
74
}
54
75
55
- ll a = pi (pow (x, 1.0 / 4 ));
56
- ll b = pi (pow (x, 1.0 / 2 ));
57
- ll c = pi (pow (x, 1.0 / 3 ));
76
+ ll a = pi (isqrt ( isqrt (x) ));
77
+ ll b = pi (isqrt (x ));
78
+ ll c = pi (icbrt (x ));
58
79
59
80
ll res = phi (x, a) + (b+a-2 )*(b-a+1 )/2 ;
60
81
61
82
for (ll i = a+1 ; i <= b; i++) {
62
83
ll w = x / primes[i-1 ];
63
- ll b_i = pi (pow (w, 1.0 / 2 ));
84
+ ll b_i = pi (sqrt (w ));
64
85
res -= pi (w);
65
86
66
87
if (i <= c) {
@@ -73,7 +94,6 @@ ll pi(ll x) {
73
94
pi_cache[x] = res;
74
95
return res;
75
96
}
76
-
77
97
int main () {
78
98
ll n;
79
99
cin >> n;
You can’t perform that action at this time.
0 commit comments