-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathCounting Coprime Pairs.cpp
59 lines (51 loc) · 1.13 KB
/
Counting Coprime Pairs.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxX = 1e6+1;
ll ans;
int N, dp[maxX];
bool b[maxX];
vector<int> primes;
void init(){
fill(b+2, b+maxX, true);
for(int i = 2; i*i < maxX; i++)
if(b[i])
for(int j = i*i; j < maxX; j += i)
b[j] = false;
for(int i = 2; i < maxX; i++)
if(b[i])
primes.push_back(i);
}
void compute(int x){
vector<int> pf;
for(int p : primes){
if(x == 1) break;
else if(b[x]){
pf.push_back(x);
break;
}
if(x % p) continue;
pf.push_back(p);
while(x % p == 0)
x /= p;
}
int K = (int) pf.size();
for(int mask = 0; mask < (1<<K); mask++){
int mu = 1;
for(int i = 0; i < K; i++)
if(mask&(1<<i))
mu *= pf[i];
int k = __builtin_popcount(mask);
ans += (k&1 ? -dp[mu] : dp[mu]);
dp[mu]++;
}
}
int main(){
init();
scanf("%d", &N);
for(int i = 0, x; i < N; i++){
scanf("%d", &x);
compute(x);
}
printf("%lld\n", ans);
}