# hihocoder #1867 : GCD

$$\sum_{i=1}^{n}\sum_{j=i}^{n}[i \perp j] [a_i \perp a_j]$$

\begin{aligned} &\sum_{i=1}^{n}\sum_{j=1}^{n}[i \perp j][a_i \perp a_j] \\ =&[a_1=1]+2\sum_{i=1}^{n}\sum_{j=i+1}^{n}[i \perp j][a_i \perp a_j] \\ =&[a_1=1]-2[a_1=1]+2\sum_{i=1}^{n}\sum_{j=i}^{n}[i \perp j][a_i \perp a_j] \\ \end{aligned}

$$\sum_{i=1}^{n}\sum_{j=i}^{n}[i \perp j] [a_i \perp a_j]=\frac{[a_1=1]+\sum_{i=1}^{n}\sum_{j=1}^{n}[i \perp j][a_i \perp a_j]}{2}$$

$$F(d)=\sum_{i=1}^{n}\sum_{j=1}^{n}[d \mid i][d \mid j] [a_i \perp a_j]$$

$$f(d)=\sum_{i=1}^{n}\sum_{j=1}^{n}[\gcd(i,j)=d][a_i \perp a_j]=(F \times \mu)(d)$$

\begin{aligned} F(d)=&\sum_{i=1}^{n}\sum_{j=1}^{n}[d \mid i][d \mid j] [a_i \perp a_j] \\ =&\sum_{t=1}^{n}\mu(t) \left(\sum_{i=1}^{n} [d \mid i] [t \mid a_i] \right)^2 \end{aligned}

 1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 const int N = 2e5 + 10;
5 vector<int> g[N];
6 ll ans, f[N], F[N], cnt[N];
7 int n, mu[N], pri[N], tot, vis[N], a[N], fafa[N];
8
9 int main() {
10     scanf("%d", &n);
11     for(int i = 1 ; i <= n ; ++ i) scanf("%d", &a[i]);
12
13     mu[1] = 1;
14     for(int i = 2 ; i <= n ; ++ i) {
15         if(!vis[i]) {
16             mu[i] = -1;
17             pri[++ tot] = i;
18         }
19         for(int j = 1 ; j <= tot && i * pri[j] <= n ; ++ j) {
20             vis[i * pri[j]] = 1;
21             if(i % pri[j] == 0) break;
22             mu[i * pri[j]] = -mu[i];
23         }
24     }
25
26     for(int i = 1 ; i <= n ; ++ i)
27         for(int j = i ; j <= n ; j += i)
28             g[j].push_back(i);
29
30     for(int d = 1 ; d <= n ; ++ d) {
31         for(int i = d ; i <= n ; i += d) {
32             for(int j = 0 ; j < g[a[i]].size() ; ++ j) {
33                 ++ cnt[g[a[i]][j]];
34             }
35         }
36
37         int T = ++ fafa[0];
38         for(int i = d ; i <= n ; i += d) {
39             for(int j = 0 ; j < g[a[i]].size() ; ++ j) {
40                 int x = g[a[i]][j];
41                 if(fafa[x] == T) continue;
42                 fafa[x] = T;
43                 f[d] += mu[x] * cnt[x] * cnt[x];
44                 cnt[x] = 0;
45             }
46         }
47     }
48
49 //    for(int i = 1 ; i <= n ; ++ i)
50 //        for(int j = i ; j <= n ; j += i)
51 //            F[i] += f[j] * mu[j / i];
52 //    ans = (F[1] + (a[1] == 1)) / 2;
53
54     for(int i = 1 ; i <= n ; ++ i)
55         ans += f[i] * mu[i];
56     ans = (ans + (a[1] == 1)) / 2;
57     printf("%lld\n", ans);
58 }
hihocoder #1867 : GCD

posted @ 2018-12-17 20:59 KingSann 阅读(...) 评论(...) 编辑 收藏