Cod sursa(job #1005477)

Utilizator nicuvladNicu Vlad nicuvlad Data 5 octombrie 2013 08:04:41
Problema Pairs Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <fstream>
#include <vector>
#define N 100001
#define M 1000001
using namespace std;

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

bool verif[M];
int prime[N], pi, cont[M];
vector <int> divi;

void ciur()
{
	long long i, j;
	for(i=2;i<M;i++)
	{
		if(!verif[i])
		{
			prime[++pi]=i;
			for(j=i*i;j<M;j+=i)
			{
				verif[j]=true;
			}
		}
	}
}

int main()
{
	int n, x, i, j, ind, sz, semn, nr;
	long long sol;
	fin>>n;
	sol=1LL*n*(n-1)/2;
	ciur();
	for(ind=0;ind<n;ind++)
	{
		fin>>x;
		divi.clear();
		for(i=1;x>1&&prime[i]*prime[i]<=x;i++)
		{
			if(x%prime[i]==0)
			{
				divi.push_back(prime[i]);
				while(x%prime[i]==0) x/=prime[i];
			}
		}
		if(x>1) divi.push_back(x);
		sz=divi.size();
		for(i=1;i<(1<<sz);i++)
		{
			nr=semn=1;
			for(j=0;j<sz;j++)
			{
				if(i&(1<<j))
				{
					semn=-semn;
					nr*=divi[j];
				}
			}
			sol+=cont[nr]*semn;
			cont[nr]++;
		}
	}
	fout<<sol;
}