D. Steps to One
设f[i]f[i]f[i]为gcd\gcdgcd为iii,还需要多少个数,那么有f[i]=1+∑j=1mf[gcd(i,j)]mf[i] = 1 + \frac{\sum\limits_{j = 1} ^{m} f[\gcd(i, j)]}{m}f[i]=1+mj=1∑mf[gcd(i,j)],
f[1]=0f[1] = 0f[1]=0,考虑化简∑j=1mf[gcd(i,j)]\sum\limits_{j = 1} ^{m} f[\gcd(i, j)]j=1∑mf[gcd(i,j)],
∑d∣if[d]∑j=1m[gcd(i,j)=d]给定i,d,求∑j=1m[gcd(i,j)=d]∑j=1md[gcd(id,j)=1]∑k∣idμ(k)mkd则原式为∑d∣if[d]∑k∣idμ(k)mkd\sum_{d \mid i} f[d] \sum_{j = 1} ^{m}[\gcd(i, j) = d]\\ 给定i, d,求\sum_{j = 1} ^{m}[\gcd(i, j) = d]\\ \sum_{j = 1} ^{\frac{m}{d}}[\gcd(\frac{i}{d}, j) = 1]\\ \sum_{k \mid \frac{i}{d}} \mu(k) \frac{m}{kd}\\ 则原式为 \sum_{d \mid i} f[d] \sum_{k \mid \frac{i}{d}} \mu(k) \frac{m}{kd}\\ d∣i∑f[d]j=1∑m[gcd(i,j)=d]给定i,d,求j=1∑m[gcd(i,j)=d]j=1∑dm[gcd(di,j)=1]k∣di∑μ(k)kdm则原式为d∣i∑f[d]k∣di∑μ(k)kdm
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10, mod = 1e9 + 7;int f[N], prime[N], mu[N], m, cnt;vector<int> fac[N];bool st[N];int quick_pow(int a, int n) {int ans = 1;while (n) {if (n & 1) {ans = 1ll * ans * a % mod;}a = 1ll * a * a % mod;n >>= 1;}return ans;
}inline int inv(int a) {return quick_pow(a, mod - 2);
}void init() {mu[1] = 1;for (int i = 2; i < N; i++) {if (!st[i]) {prime[++cnt] = i;mu[i] = mod - 1;}for (int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {st[i * prime[j]] = 1;if (i % prime[j] == 0) {break;}mu[i * prime[j]] = (mod - mu[i]) % mod;}}for (int i = 1; i < N; i++) {for (int j = i; j < N; j += i) {fac[j].push_back(i);}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);init();scanf("%d", &m);int inv_m = inv(m);for (int i = 1; i <= m; i++) {for (auto &d : fac[i]) {if (d == i) {continue;}int cur = 0;for (auto &k : fac[i / d]) {cur = (cur + 1ll * mu[k] * (m / (k * d)) % mod) % mod;}f[i] = (f[i] + 1ll * f[d] * cur % mod) % mod;}f[i] = 1ll * (m + f[i]) * inv(m - m / i) % mod;}int ans = 0;for (int i = 1; i <= m; i++) {ans = (ans + f[i]) % mod;}cout << (1 + 1ll * ans * inv_m) % mod << "\n";return 0;
}