Cod sursa(job #629582)

Utilizator AndreiRSStatescu Andrei Rares AndreiRS Data 3 noiembrie 2011 15:42:41
Problema Pairs Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream fi ("pairs.in");
ofstream fo ("pairs.out");

const int DIM = 1<<20;
vector <int> V[DIM];
int ap[DIM], D[DIM], valmax, N;
long long S;

void cit ()
{
	fi >> N;
	for (int i = 1, x; i <= N; i++)
	{	
		fi >> x;
		ap[x] = 1;
		valmax = max (valmax, x);
	}	
}

void ciur ()
{
	for (int i = 2; i <= valmax; i++)
		if (V[i].empty())
			for (int j = i; j <= valmax; j += i)
			{
				D[i] += ap[j];
				V[j].push_back (i);
			}
}

void pairs ()
{
	int i, j;
	long long x;
	for (i = 2; i <= valmax; i++)
	{
		x = i;
		for (j = 0; j < V[i].size(); j++)
			x /= V[i][j];
		if (x != 1)
			continue;
		
		x = (long long) D[i] * (D[i] - 1) / 2;
		if (j & 1)
			S += x;
		else
			S -= x;		
	}
	
}

void afi ()
{
	fo << S;
}

int main ()
{
	cit ();
	ciur ();
	pairs ();
	afi ();
	return 0;
}