∑ i = 1 n ∑ j = 1 n gcd ( a i , a j ) gcd ( i , j ) ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 n d gcd ( a i d , a j d ) [ gcd ( i , j ) = 1 ] ∑ d = 1 n d ∑ k = 1 n d μ ( k ) ∑ i = 1 n k d ∑ j = 1 n k d gcd ( a i k d , a j k d ) T = k d ∑ T = 1 n ∑ i = 1 n T ∑ j = 1 n T gcd ( a i T , a j T ) ∑ d ∣ T d μ ( T d ) ∑ T = 1 n ϕ ( T ) ∑ i = 1 n T ∑ j = 1 n T gcd ( a i T , a j T ) \sum_{i = 1} ^{n} \sum_{j = 1} ^{n} \gcd(a_i, a_j) \gcd(i, j)\\ \sum_{d = 1} ^{n} d \sum_{i = 1} ^{\frac{n}{d}} \sum_{j = 1} ^{\frac{n}{d}} \gcd(a_{id}, a_{jd})[\gcd(i, j) = 1]\\ \sum_{d = 1} ^{n} d \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \sum_{i = 1} ^{\frac{n}{kd}} \sum_{j = 1} ^{\frac{n}{kd}} \gcd(a_{i kd}, a_{jkd})\\ T = kd\\ \sum_{T = 1} ^{n} \sum_{i = 1} ^{\frac{n}{T}} \sum_{j = 1} ^{\frac{n}{T}} \gcd(a_{iT}, a_{jT}) \sum_{d \mid T} d \mu(\frac{T}{d})\\ \sum_{T = 1} ^{n} \phi(T) \sum_{i = 1} ^{\frac{n}{T}} \sum_{j = 1} ^{\frac{n}{T}} \gcd(a_{iT}, a_{jT})\\ i=1∑nj=1∑ngcd(ai,aj)gcd(i,j)d=1∑ndi=1∑dnj=1∑dngcd(aid,ajd)[gcd(i,j)=1]d=1∑ndk=1∑dnμ(k)i=1∑kdnj=1∑kdngcd(aikd,ajkd)T=kdT=1∑ni=1∑Tnj=1∑Tngcd(aiT,ajT)d∣T∑dμ(dT)T=1∑nϕ(T)i=1∑Tnj=1∑Tngcd(aiT,ajT)
我们考虑设 f ( n , T ) = ∑ i = 1 n T ∑ j = 1 n T gcd ( a i T , a j T ) f(n, T) = \sum\limits_{i = 1} ^{\frac{n}{T}} \sum\limits_{j = 1} ^{\frac{n}{T}} \gcd(a_{iT}, a_{jT}) f(n,T)=i=1∑Tnj=1∑Tngcd(aiT,ajT), g ( x ) g(x) g(x)为 i ∈ [ T , 2 T , … , n T T ] i \in [T, 2T, \dots, \frac{n}{T} T] i∈[T,2T,…,TnT]时 x x x的出现次数。
f ( n , T ) = ∑ i = 1 m ∑ j = 1 m g ( i ) g ( j ) gcd ( i , j ) , ( m = 1 0 5 ) ∑ d = 1 m d ∑ i = 1 m d ∑ j = 1 m d g ( i d ) g ( j d ) [ gcd ( i , j ) = 1 ] ∑ d = 1 m d ∑ k = 1 n d μ ( k ) ( ∑ i = 1 m k d g ( i k d ) ) 2 T = k d ∑ T = 1 m ϕ ( T ) ( ∑ i = 1 m T g ( i T ) ) 2 f(n, T) = \sum_{i = 1} ^{m} \sum_{j = 1} ^{m} g(i) g(j) \gcd(i, j), (m = 10 ^ 5)\\ \sum_{d = 1} ^{m} d \sum_{i = 1} ^{\frac{m}{d}} \sum_{j = 1} ^{\frac{m}{d}} g(id) g(jd) [\gcd(i, j) = 1]\\ \sum_{d = 1} ^{m} d \sum_{k = 1} ^{\frac{n}{d}} \mu(k) \left( \sum_{i = 1} ^{\frac{m}{kd}} g(ikd) \right) ^ 2\\ T = kd\\ \sum_{T = 1} ^{m} \phi(T) \left( \sum_{i = 1} ^{\frac{m}{T}} g(iT) \right) ^ 2\\ f(n,T)=i=1∑mj=1∑mg(i)g(j)gcd(i,j),(m=105)d=1∑mdi=1∑dmj=1∑dmg(id)g(jd)[gcd(i,j)=1]d=1∑mdk=1∑dnμ(k)⎝⎛i=1∑kdmg(ikd)⎠⎞2T=kdT=1∑mϕ(T)⎝⎛i=1∑Tmg(iT)⎠⎞2
考虑重新定义 g ( n ) g(n) g(n)表示为是 n n n的倍数的数字有多少个,则上式可以直接写成:
∑ T = 1 m ϕ ( T ) g ( T ) 2 \sum_{T = 1} ^{m} \phi(T) g(T) ^ 2\\ T=1∑mϕ(T)g(T)2
由此我们可以在 O ( n log 2 n ) O(n \log ^ 2n) O(nlog2n)的时间内完成这题。
#include
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7;
int prime[N], phi[N], a[N], n, cnt;
int sum[N], m;
bool st[N];
vector<int> fac[N];
inline int add(int x, int y) {
return x + y < mod ? x + y : x + y - mod;
}
inline int sub(int x, int y) {
return x >= y ? x - y : x - y + mod;
}
void init() {
phi[1] = 1;
for (int i = 2; i < N; i++) {
if (!st[i]) {
prime[++cnt] = i;
phi[i] = i - 1;
}
for (int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {
st[i * prime[j]] = 1;
if (i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
for (int i = 1; i < N; i++) {
for (int j = i; j < N; j += i) {
fac[j].push_back(i);
}
}
}
int f(int n, int T) {
int ans = 0;
for (int i = T; i <= n; i += T) {
for (auto it : fac[a[i]]) {
ans = sub(ans, 1ll * phi[it] * sum[it] % mod * sum[it] % mod);
sum[it]++;
ans = add(ans, 1ll * phi[it] * sum[it] % mod * sum[it] % mod);
}
}
for (int i = T; i <= n; i += T) {
for (auto it : fac[a[i]]) {
sum[it]--;
}
}
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
init();
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int ans = 0;
for (int T = 1; T <= n; T++) {
ans = add(ans, 1ll * phi[T] * f(n, T) % mod);
}
printf("%d\n", ans);
return 0;
}