Luckyleaves's Blog

stay hungry,stay foolish greedy and lucky

CF1139D Steps to One

期望+莫比乌斯反演。

期望的题,每次都这么神秘啊。

Solution

首先我们先把式子写出来,令$E$是期望,$P$是概率。
$$
\begin{aligned}
E(len) &= \sum_{i \ge 1} P(len=i) \cdot i \\
&= \sum_{i \ge 1} P(len=i) \sum_{j=1}^i \\
&= \sum_{j\ge 1}\sum_{i\ge j} P(len=i) \\
&= \sum_{i\ge 1} P(len \ge i) \\
&= 1 + \sum_{i\ge 1} P(len > i) \\
\end{aligned}
$$
最后的状态是$\gcd_{i=1}^{len}a_i=1$,于是又可推得:
$$
\begin{aligned}
P(len>i) &= P((\gcd_{j=1}^{i}a_i) > 1)\\
&= 1 - P((\gcd_{j=1}^ia_i)=1) \\
&= 1 - \frac{\sum_{a_1=1}^m\sum_{a_2=1}^m\cdots\sum_{a_n=1}^m\sum_{d|\gcd_{j=1}^{m}a_j}\mu(d)}{m^i}\\
&= 1- \frac{\sum_{d=1}^m\mu(d)\left \lfloor \frac{m}{d} \right \rfloor^i}{m^i}\\
&= \frac{\sum_{d=2}^m\mu(d)\left \lfloor \frac{m}{d} \right \rfloor^i}{m^i}
\end{aligned}
$$
再带回原式可得:
$$
\begin{aligned}
E(len) &= 1 + \sum_{i\ge 1} P(len > i)\\
&= 1 - \sum_{i\ge 1} \frac{\sum_{d=2}^m\mu(d)\left \lfloor \frac{m}{d} \right \rfloor^i}{m^i}\\
&= 1 - \sum_{d=2}^m \mu(d) \sum_{i \ge 1} (\frac{\left \lfloor \frac{m}{d} \right \rfloor}{m})^i \\
&= 1 - \sum_{d=2}^m \mu(d) \frac{\left \lfloor \frac{m}{d} \right \rfloor}{m-\left \lfloor \frac{m}{d} \right \rfloor}
\end{aligned}
$$
关于最后一步是生成函数的无限项求值。

Code

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
#include<bits/stdc++.h>
#include<algorithm>
#define LL long long
#define PII pair<LL, int>
using namespace std;
template <class T>
inline void read(T &res)
{
res = 0; bool flag = 0;
char c = getchar();
while('0' > c || c > '9') { if(c == '-') flag = 1; c = getchar();}
while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar();
if(flag) res = -res;
}
template <class T, class ...Arg>
inline void read(T &res, Arg &...com){ read(res), read(com...);}
template <class T>
void out(T res)
{
if(res > 9) out(res / 10);
putchar(res % 10 + '0');
}
template <class T>
inline void write(T res)
{
if(res < 0) putchar('-'), res = -res;
out(res);
}
template <>
inline void write(char c) { putchar(c); }
template <>
inline void write(const char *s){ while(*s) putchar(*s ++); }
template <class T, class ...ARC>
inline void write(T res, ARC ...com) { write(res), write(com...); }
const int N = 1e5 + 5, mod = 1e9 + 7;
int n;
int mu[N], primes[N], cnt, st[N], inv[N];
void init()
{
mu[1] = 1;
for(int i = 2;i < N;i ++)
{
if(!st[i]) primes[++ cnt] = i, mu[i] = -1;
for(int j = 1;j <= cnt && primes[j] * i < N;j ++)
{
st[primes[j] * i] = 1;
if(i % primes[j] == 0)
{
mu[primes[j] * i] = 0;
break;
}
mu[primes[j]* i] = -mu[i];
}
}
inv[1] = 1;
for(int i = 2;i < N;i ++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
}
int ans;
int main()
{
read(n);
init();
ans = 1;
for(int i = 2;i <= n;i ++)
ans = (ans - 1ll * mu[i] % mod * (n / i) % mod * inv[n - n / i] % mod) % mod, ans = (ans + mod) % mod;
write(ans);
return 0;
}