Cod sursa(job #1408400)

Utilizator CostanMiriamCostan Miriam CostanMiriam Data 30 martie 2015 00:19:47
Problema Pairs Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <fstream>
#include <vector>

#define NMAX 1000001
#define DIM 100001

using namespace std;

ifstream fin("pairs.in");
ofstream fout("pairs.out");

int n;

bool vis[NMAX];

int ok[NMAX];

long long sol;

int main() {

	fin >> n;

	for (int i = 1; i <= n; i++) {

		int x;

		fin >> x;

		vis[x] = 1;

	}

	for (int i = 2; i < NMAX; i++) {


		if (ok[i]) {

			if (ok[i] == -1)
				continue;

			int nrc = 0;

			for (int j = i; j < NMAX; j += i) {

				nrc += vis[j];

			}

			if (ok[i] % 2 == 1)
				sol += 1LL * nrc * (nrc - 1) / 2;
			else
				sol -= 1LL * nrc * (nrc - 1) / 2;

			continue;

		}

		int nrc = 0;

		for (int j = i; j < NMAX; j += i) {

			nrc += vis[j];

		}

		for (int j = i + i; j < NMAX; j += i) {

			if (ok[j] != -1)
				ok[j]++;

			if (j % (i * i) == 0)
				ok[j] = -1;

		}

		sol += 1LL * nrc * (nrc - 1) / 2;
	
	}

	fout << 1LL * (n - 1) * n / 2 - sol << "\n";

	return 0;

}

//Trust me, I'm the Doctor!
//Miriam e tare!