-
Notifications
You must be signed in to change notification settings - Fork 2
/
moebius.cpp
150 lines (131 loc) · 3.13 KB
/
moebius.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// added
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
// before: ss[i]: 集合族iの共通部分の大きさ
// after : ss[i]: 集合族iに含まれていて、集合族~iに含まれない部分の大きさ
void moebius(vector<int> &ss) {
int N = ss.size();
int n = 0;
while (N > 1) {
n++;
N >>= 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << n); j++) {
if (!(j & (1 << i))) {
ss[j] -= ss[j | (1 << i)];
}
}
}
}
int intsqrt(int n) {
int l = 0, r = n + 1;
while (l < r - 1) {
int m = (l + r) / 2;
if (__int128_t(m) * m <= n) {
l = m;
} else {
r = m;
}
}
return l;
}
int cntsqrt(int l, int r) {
return intsqrt(r) - intsqrt(l - 1);
}
int cntpx(int l, int r, int p) {
return r / p - (l - 1) / p;
}
int get_min_p(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return i;
}
}
return n;
}
int cntpxsqrt(int l, int r, int p) {
int q = 1;
while (p != 1) {
int k = get_min_p(p);
int cnt = 0;
while (p % k == 0) {
p /= k;
cnt++;
}
for (int i = 0; i < (cnt + 1) / 2; i++) {
q *= k;
}
}
return intsqrt(r) / q - intsqrt(l - 1) / q;
}
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int lcm(int a, int b) {
return a / gcd(a, b) * b;
}
int cntrange(int l, int r, int zone) {
if (r < l) return 0;
vector<int> ss(1 << zone);
vector<int> ps;
for (int i = 2; i <= zone; i++) {
ps.push_back(i);
}
for (int i = 0; i < (1 << zone); i++) {
int p = 1;
for (int j = 0; j < zone - 1; j++) {
if ((i >> j) & 1) {
p = lcm(p, ps[j]);
}
}
if ((i >> (zone - 1)) & 1) {
// sqrt
ss[i] = cntpxsqrt(l, r, p);
if (p == 1) {
assert(cntpxsqrt(l, r, p) == cntsqrt(l, r));
}
} else {
ss[i] = cntpx(l, r, p);
}
}
assert(ss[0] == r - l + 1);
moebius(ss);
int cnt = r - l + 1, tmp = 0;;
for (int i = 0; i < (1 << zone); i++) {
assert(ss[i] >= 0);
if (__builtin_popcount(i) & 1) {
cnt -= ss[i];
} else {
tmp += ss[i];
}
}
assert(cnt >= 0);
assert(cnt == tmp);
return cnt;
}
int main() {
int n;
cin >> n;
int B = 1;
for (int i = 0; i < 18; i++) {
B *= 10;
}
if (n <= B) {
cout << intsqrt(n) << endl;
return 0;
}
int cnt = intsqrt(B);
cnt += cntrange(B + 1, min(n, 2 * B), 1);
cnt += cntrange(2 * B + 1, min(n, 3 * B), 2);
cnt += cntrange(3 * B + 1, min(n, 4 * B), 3);
cnt += cntrange(4 * B + 1, min(n, 5 * B), 4);
cnt += cntrange(5 * B + 1, min(n, 6 * B), 5);
cnt += cntrange(6 * B + 1, min(n, 7 * B), 6);
cnt += cntrange(7 * B + 1, min(n, 8 * B), 7);
cnt += cntrange(8 * B + 1, min(n, 9 * B), 8);
cnt += cntrange(9 * B + 1, n, 9);
cout << cnt << endl;
}