Pagini recente » Cod sursa (job #1196592) | Cod sursa (job #2140179) | Cod sursa (job #693852) | Cod sursa (job #2536974) | Cod sursa (job #196232)
Cod sursa(job #196232)
#include <stdio.h>
#include <bitset>
using namespace std;
#define NMAX 100010
#define SQRT 1010
#define LL long long
int LIM = 0;
int N;
int a[NMAX];
int b[NMAX];
int C[40];
bitset <SQRT> pr;
int npr[1010];
bitset <1000010> jeg;
int main()
{
int i, j;
freopen("pairs.in", "r", stdin);
freopen("pairs.out", "w", stdout);
scanf("%d", &N);
for (i = 1; i <= N; i++) {
scanf("%d", &a[i]);
if (a[i] > LIM) LIM = a[i];
jeg[a[i]] = 1;
}
pr[1] = 1;
for (i = 2; i <= SQRT; i++) {
if (pr[i] == 1) continue;
npr[++npr[0]] = i;
for (j = i * i; j <= SQRT; j += i) pr[j] = 1;
}
int e, q, nr;
LL rez = 0;
for (i = 2; i <= LIM; i++) {
C[0] = 0; e = 1; q = i;
for (j = 1; j <= npr[0] && npr[j] * npr[j] <= q && e; j++) {
if (q % npr[j] == 0) {
C[++C[0]] = i;
q /= npr[j];
if (q % npr[j] == 0) e = 0;
}
}
if (!e) continue;
if (q > 1) C[++C[0]] = q;
nr = 0;
for (j = i; j <= LIM; j += i) nr += jeg[j] == 1;
if (C[0] & 1) rez += (LL) nr * (nr-1) / 2;
else rez -= (LL) nr * (nr-1) / 2;
}
printf("%lld\n", (LL) N * (N - 1) / 2 - rez);
return 0;
}