Pagini recente » Cod sursa (job #839808) | Cod sursa (job #2115778) | Cod sursa (job #1992359) | Cod sursa (job #2275526) | Cod sursa (job #3220278)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("pairs.in");
ofstream fout("pairs.out");
bitset <1000005> c;
vector <int> prime;
long long rez;
int f[1000002];
int fp[12];
void ciur(int n){
c[0] = c[1] = 1;
for(int i = 4; i <= n; i += 2) c[i] = 1;
for(int i = 3; i * i <= n; i++){
if(!c[i]){
for(int j = i * i; j <= n; j += 2 * i) c[j] = 1;
}
}
for(int i = 1; i <= n; i++) if(!c[i]) prime.push_back(i);
}
void solve(int n, int t){
for(int e = 1; e < (1 << t); e++){
int p = 1,nrb = 0;
for(int i = 0; i < t; i++){
if((1 << i) & e){
p *= fp[i];
nrb++;
}
}
if(nrb & 1) rez = rez - f[p];
else rez = rez + f[p];
}
}
int main()
{
int n,i,x;
fin >> n;
ciur(1e6);
rez = 1LL * n * (n - 1) / 2;
for(i = 1; i <= n; i++){
fin >> x;
int e = x,t = 0;
for(auto p : prime){
if(e <= 1) break;
if(p * p > e) p = e;
if(e % p == 0){
fp[t++] = p;
while(e % p == 0) e /= p;
}
}
solve(x,t);
int d = 1;
for(d = 1; d * d < x; d++){
if(x % d == 0){
f[d]++;
f[x / d]++;
}
}
if(d * d == x) f[d]++;
}
fout << rez;
return 0;
}