Cod sursa(job #774679)

Utilizator BarracudaFMI-Alex Dobrin Barracuda Data 6 august 2012 13:17:20
Problema Pairs Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include<fstream>
#define dim 100070
using namespace std;


ifstream f("pairs.in");
ofstream g("pairs.out");

bool  p[dim],NU[dim];
long long C[dim];

int main (){
	long long res,x,n,i,j,MAX;
	f>>n;
	long long  nrdp[dim];
	MAX=-1;
	for(i=1;i<=n;i++){
		f>>x;
		C[x]=1;
		if(MAX<x)
			MAX=x;
	}
	res=(long long)n*(n-1)/2;
	for(i=2;i<MAX;++i){
		
		for(j=2*i;j<MAX;j+=i){
			
			C[i]+=C[j];
			
		}
		if(!p[i]){
			nrdp[i]=1;
			p[i]=1;
			for(j=2*i;j<MAX;j+=i) {
				p[j]=1;
				if((j/i)%i==0)
					NU[j]=1;
				nrdp[j]^=1;
			}
			
		}
		if(!NU[i]){
			
			if(nrdp[i])
				res-=(long long )C[i]*(C[i]-1)/2;
			else
				res+=(long long )C[i]*(C[i]-1)/2;
			
		}
	}
	
	g<<res<<"\n";
	return 0;
}