Cod sursa(job #232407)

Utilizator hello2alex gen2 hello2 Data 15 decembrie 2008 10:38:32
Problema Pairs Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function
#include <algorithm>
#include <math.h>

using namespace std;

long cmmdc(long a, long b)
{
	return ( b == 0 ? a : cmmdc(b, a % b) );
}

int main()
{
	//LARGE_INTEGER t1;
	//QueryPerformanceCounter(&t1);
	ifstream indata; // indata is like cin
	ofstream outdata;
	indata.open("pairs.in"); // opens the file
	outdata.open("pairs.out");
	if(!indata) { // file couldn't be opened
		//cerr << "Error: file could not be opened" << endl;
		exit(1);
	}
	long n;
	indata >> n;
	long* vec = new long[n];
	//cout << "n=" << n << endl;
	for (long i = 0; i < n; i++)
	{
		indata >> vec[i];
		//cout << vec[i] << " ";
	}
	//cout << endl;
	indata.close();
	long nr = 0;
	for (long i = 0; i < n; i++)
	{
		for (long j = i + 1; j < n; j++)
		{
			if (cmmdc(vec[i], vec[j]) == 1)
				++nr;
		}
	}
	//cout << "nr=" << nr << endl;
	outdata << nr;
	outdata.close();
	//LARGE_INTEGER t2;
	//QueryPerformanceCounter(&t2);
	//cout << "time=" << t2.QuadPart - t1.QuadPart << endl;
	//cout << t1.HighPart << " " <<t1.LowPart <<" " << endl;
	//cout << t2.HighPart << " " <<t2.LowPart <<" " << endl;
	return 0;
}