Cod sursa(job #968164)

Utilizator danlexDan Alexandru danlex Data 1 iulie 2013 20:47:32
Problema Fractii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;

int n, out, v[1000002], p[1000002];
bool DEBUG = true;
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){
        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(){
    compute_primes(n);
    s = 1;
    for(int i = 2; i <= n; i++){
        s += 2 * phi(i);
    }
}


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