Cod sursa(job #1229118)

Utilizator patrick_vladPatrick Vlad patrick_vlad Data 16 septembrie 2014 15:19:21
Problema Fractii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>
using namespace std;

ifstream f1 ("fractii.in"); //INTRARE
ofstream f2 ("fractii.out"); //IESIRE

int cmmdc(int x, int y)
{
    //int t;
    //while (b != 0)
    while(x!=y)
		if(x>y)
			x=x-y;
		else
			y=y-x;
    return x;
}
int main(){
	long long n;
	long long s;
	f1>>n;
	s = n * 2 - 1;
	if(n % 2 == 0) s += n - 2;
	else s += n - 1;
	for(int i = 3; i <= n; i++){
		if(i % 2 == 0){
			for(int j = i + 1; j <= n; j += 2){
				if(cmmdc(i,j) == 1) s += 2;
			}
		} else {
			for(int j = i + 1; j <= n; j++){
				if(cmmdc(i,j) == 1) s += 2;
			}
		}
	}
	f2<<(int)s<<"\n";
	return 0;
}