Cod sursa(job #109246)

Utilizator webspiderDumitru Bogdan webspider Data 25 noiembrie 2007 09:35:26
Problema Pairs Scor 0
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasele 11-12 Marime 0.75 kb
#include <iostream>
#include <stdio.h>

using namespace std;

const int maxM = 1000001;

bool apM[ maxM ];
bool ciur[ maxM ];

int n;
int nec;
int vmax;
long long sol;
int aux;
int nr;
int i,j;

int main()
{
	freopen("pairs.in","r",stdin);
	freopen("pairs.out","w",stdout);

	scanf("%d\n", &n );
	for ( i = 1; i <= n; i++ ) {
		scanf("%d\n", &aux );
		if ( !apM[ aux ] ) nr++;
		apM[ aux ] = 1;
		if ( aux > vmax ) vmax = aux;
	}
	sol = ( long long ) ( nr*(nr-1) )/2;

	for ( i = 2; i <= vmax; i++ ) {
		if ( !ciur[ i ] ) {
			nec = 0;
			if ( apM[ i ] ) nec = 1;
			for ( j = i+i; j <= vmax; j += i ) {
				ciur[ j ] = 1;
				if ( apM[ j ] ) {
					sol -= nec;
					nec++;
				}
			}
		}
	}

	printf("%lld\n", sol );

	fclose(stdin);
	fclose(stdout);

	return 0;
}