-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobius_gcd.cpp
More file actions
234 lines (194 loc) · 4.44 KB
/
Copy pathmobius_gcd.cpp
File metadata and controls
234 lines (194 loc) · 4.44 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//timus 1816
#include <bits/stdc++.h>
#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define in(x) int (x); input((x));
#define x first
#define y second
typedef int itn;
#define next next12345
#define prev prev12345
#define left lefdsf232
#define right rig43783
#define x1 x12345
#define y1 y12345
using namespace std;
template<typename T>
T gcd(T x, T y) {
while (y > 0) {
x %= y;
swap(x, y);
}
return x;
}
template<class _T>
inline _T sqr(const _T &x) {
return x * x;
}
template<class _T>
inline string tostr(const _T &a) {
ostringstream os("");
os << a;
return os.str();
}
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
const long double PI = 3.1415926535897932384626433832795L;
template<typename T>
inline void input(T &a) {
static int ed;
a = 0;
while (!isdigit(ed = getchar()) && ed != '-') { }
char neg = 0;
if (ed == '-') {
neg = 1;
ed = getchar();
}
while (isdigit(ed)) {
a = 10 * a + ed - '0';
ed = getchar();
}
if (neg) a = -a;
}
template<typename T = int>
inline T nxt() {
T res;
input(res);
return res;
}
mt19937 generator;
long long mod = 1000000007;
void fib(unsigned long long n, long long &x, long long &y) {
if (n == 1) {
x = 0, y = 1;
return;
}
if (n & 1ull) {
fib(n - 1, y, x);
y += x;
y %= mod;
} else {
long long a, b;
fib(n / 2, a, b);
x = (a * a + b * b) % mod;
y = (2 * a + b) * b % mod;
}
}
long long pw(long long a, long long n) {
long long res = 1;
while (n) {
if (n & 1ull) {
res = res * a % mod;
}
a = a * a % mod;
n >>= 1;
}
return res;
}
map <long long, int> dp;
map <long long, vector <long long> > divisors;
vector <long long> & getDivs(long long x) {
if (divisors.count(x)) {
return divisors[x];
}
vector <long long> divs;
long long d;
for (d = 1; d * d < x; ++d) {
if (x % d == 0) {
divs.push_back(d);
divs.push_back(x / d);
}
}
if (d * d == x) {
divs.push_back(d);
}
return divisors[x] = divs;
}
int mu(long long x) {
if (x == 1) {
return 1;
}
if (dp.count(x)) {
return dp[x];
}
vector <long long> & divs = getDivs(x);
int res = 0;
for (long long d : divs) {
if (d == x) {
continue;
}
res -= mu(d);
}
return dp[x] = res;
}
long long get(long long r, long long g, long long x) {
vector <long long> & divs = getDivs(x);
long long res = 0;
for (long long d : divs) {
if (d % g == 0) {
res += r / d * mu(d / g);
}
}
return res;
}
long long get2(long long r, long long g, long long x) {
long long res = 0;
for (long long i = 1; i <= r; ++i) {
if (gcd(i, x) == g) {
res += 1;
}
}
return res;
}
map <long long, long double> res;
long long magic = 1000000000000000000ll;
long double solve(long long n) {
if (res.count(n)) {
return res[n];
}
const auto & divs = getDivs(n);
if (divs.size() == 2) {
return res[n] = 0;
}
long double l = 1;
long double r = 1;
for (long long d : divs) {
if (d == 1 || d == n) {
l -= (long double)get(magic, d, n) / magic;
continue;
}
r += (long double)get(magic, d, n) / magic * (solve(d) + solve(n / d));
}
return res[n] = r / l;
}
int main() {
//#define int long
#ifdef LOCAL
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#else
#define fname "race"
//freopen(fname".in", "r", stdin);
//freopen(fname".out", "w", stdout);
#endif
// for (int i = 1; i <= 50; ++i) {
// for (int j = 1; j <= 50; ++j) {
// for (int k = j; k <= 50; ++k) {
// if (k % j != 0) continue;
// cerr << i << " " << j << " " << k << " " << get(i, j, k) << " " << get2(i, j, k) << "\n";
// assert(get(i, j, k) == get2(i, j, k));
// }
// }
// }
int n = nxt();
cout << setprecision(10) << fixed;
cout << solve(n) << "\n";
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif
return 0;
}