Pagini recente » Cod sursa (job #744800) | Cod sursa (job #2415162) | Istoria paginii runda/iconcurs14/clasament | Cod sursa (job #1181721) | Cod sursa (job #213305)
Cod sursa(job #213305)
#include <stdio.h>
#include <math.h>
long n, i, j, max, k, rez, div[1000100], ap[1000100], nr[1000100];;
int main() {
freopen("pairs.in", "r", stdin);
freopen("pairs.out", "w", stdout);
scanf("%ld", &n);
max = 0;
for (i = 0; i < n; ++i){
scanf("%ld", &j);
nr[j] = 1;
if (j > max) {
max = j;
}
}
for (i = 2; i <= max; ++i){
if (div[i] > 1) {
for (j = i; j <= max; j += i){
if (nr[j]) {
++ap[i];
}
}
}
if (div[i] == 0) {
for (j = i; j <= max; j += i) {
++div[j];
if (nr[j]) {
++ap[i];
}
}
if (i < 1500) {
k = i * i;
for (j = k;j <= max; j += k) {
div[j] = -1000;
}
}
}
}
rez = 0;
for (i = 2; i <= max; ++i){
if(ap[i]) {
if (div[i] % 2 == 0) {
rez -= (ap[i] * (ap[i] - 1)) / 2;
} else {
rez += (ap[i] * (ap[i] - 1)) / 2;
}
}
}
rez = (n * (n - 1)) / 2 - rez;
printf("%ld\n", rez);
return 0;
}