Cod sursa(job #2192815)

Utilizator WebDesignbyTMGhiorghiu Ioan-Viorel WebDesignbyTM Data 7 aprilie 2018 13:32:18
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#define DM (int) 1e6
#include <bitset>
#include <fstream>
#include <vector>
using namespace std;

ifstream fi ("pairs.in");
ofstream fo ("pairs.out");
bitset <DM+2> v;
int n, a, bs[DM+2];
long long tot;

void ciur()
{
	//vanilla
	for (int i = 2; i <= DM; ++i)
		if (!bs[i])
		{
			bs[i] = 1;
			for (int j = i*2; j <= DM; j += i)
				++bs[j];
		}
	for (int i = 2; i*i <= DM; ++i)
		for (int j = i*i; j <= DM; j += i*i)
			bs[j] = 0;
}

int main()
{
	fi >> n;
	for (int i = 1; i <= n; ++i)
	{
		fi >> a;
		v[a] = 1;
	}
	ciur();
	tot = 1LL*n*(n - 1)/2;
	for (int i = 2; i <= DM; ++i)
		if (bs[i])
		{
			a = 0;
			for (int j = i; j <= DM; j += i)
				a += v[j];
			if (bs[i]&1)
		
				tot -= 1LL*a*(a - 1)/2;
			else
				tot += 1LL*a*(a - 1)/2;
		}
	fo << tot;
	return 0;
}