Cod sursa(job #1408401)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 30 martie 2015 00:20:01
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include <fstream>
#include <algorithm>
#include <vector>

#define NMAX 1000001
#define DIM 100001
#define infile "pairs.in"
#define outfile "pairs.out"

using namespace std;

ifstream fin(infile);
ofstream fout(outfile);

int n;

bool vis[NMAX];

int ok[NMAX];

long long sol;

int main() {

	fin >> n;

	int Max = 0;

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

		int x;

		fin >> x;

		vis[x] = 1;

		Max = max(Max, x);

	}

	++Max;

	sol = 1LL * n * (n - 1) / 2;

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

		if (ok[i]) {

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

			int temp = 0;

			for (int j = i; j < Max; j += i)
				temp += vis[j];

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

			continue;

		}

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

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

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

		}

		int temp = 0;

		for (int j = i; j < Max; j += i)
			temp += vis[j];

		sol -= 1LL * temp * (temp - 1) / 2;

	}

	fout << sol << "\n";

	return 0;

}

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