Cod sursa(job #196232)

Utilizator gcosminGheorghe Cosmin gcosmin Data 24 iunie 2008 21:29:29
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#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;
}