Cod sursa(job #968930)

Utilizator danlexDan Alexandru danlex Data 3 iulie 2013 01:10:07
Problema Fractii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

int n, out, v[1000002], p[1000002];
bool DEBUG = false;
long long s;

void print(){
	cout << endl;
	cout << "n: " << n << endl;
	cout << "s: " << s << endl;
	cout << endl;
}

void read(){
    ifstream fi("fractii.in");
    fi >> n;
    fi.close();
}

void write(){
    ofstream fo("fractii.out");
    fo << s;
    fo.close();

}

void compute_primes(int n){
	int i, j;
	p[0] = 0;
	p[1] = 0;
	for(i = 2; i < n; i ++){
		p[i] = 1;
	}
	i = 2;
	j = 2;
	while(i < n){
		if(DEBUG) cout << i << endl;
		j = i;
		while(j + i < n){
			j = j + i;
			p[j] = 0;	
		}
		i = i + 1;
		while(p[i] == 0 && i < n){
			i = i + 1;
		}
	}
}

int phi(int n){
	long long s;
	if (p[n] == 1) return s = n -1;
	s = n;
	for(int i = 2; i < n; i ++){
		if(p[i] == 1 && n % i == 0){
			s = (s / i) * (i - 1);
		} 
	}
	return s;
}

void compute(){
	int p;
	compute_primes(n);
	s = 0;
	for(int i = 2; i <= n; i++){
		s += p = phi(i);
		if(DEBUG) cout << i << " " << p << endl;
	}
	s = 2 * s + 1;
	print();
}

int gcd ( int a, int b )
{
  int c;
  while ( a != 0 ) {
     c = a; a = b%a;  b = c;
  }
  return b;
}

void compute2(){
	compute_primes(n);
	s = n - 1;
	for (int i = 3; i <= n; i ++){
		if(DEBUG) cout << i << endl;
		for (int j = 2; j < i; j ++){
			if (p[i] || gcd(i, j) == 1){
				s ++;
			}
		}  
	}
	s = 2 * s + 1;
	if(DEBUG) print();
}


int main(void){
    read();
    compute2();
    write();
	return 0;
}