Cod sursa(job #109593)

Utilizator mithyPopovici Adrian mithy Data 25 noiembrie 2007 12:01:25
Problema Pairs Scor 20
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasa a 10-a Marime 0.9 kb
#include <cstdio>
#include <vector>

long n;
std::vector<long> par;
std::vector<long> imp;

long cmmdc( long a, long b );
int main()
{	
	long i, j, pairs = 0, aux, aux2;
	FILE *fin;

	fin = fopen( "pairs.in", "rt" );
	fscanf( fin, "%ld", &n );

	for (i=0; i<n; i++)
	{
		fscanf( fin, "%ld", &aux );
		if ( aux % 2 == 0 )
			par.push_back( aux );
		else
			imp.push_back( aux );
	}

	long a,b;
	aux = par.size();
	aux2 = imp.size();
	for (i=0; i<aux; i++)
		for (j=0; j<aux2; j++)
			if ( cmmdc( par[i], imp[j] ) == 1 )
				pairs++;		
	
	for (i=0; i<aux2-1; i++)
		for (j=i+1; j<aux2; j++)
			if ( cmmdc( imp[i], imp[j] ) == 1 )
				pairs++;		

	FILE *fout;
	fout = fopen( "pairs.out", "wt" );
	fprintf( fout, "%ld\n", pairs );
	fclose( fout );

	return 0;
}

long cmmdc( long a, long b )
{
	long r ;
	while ( b )
	{
		r = a % b;
		a = b;
		b = r;
	}
	return a;
}