Cod sursa(job #1427787)

Utilizator RaresCRares Cristian RaresC Data 3 mai 2015 01:33:16
Problema Fractii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
using namespace std;
#define MAX 1000

int N;
bool not_prime[MAX];
vector<int> primes;
int n_primes;

int totient(int n) {
    int original = n;
    double ans = 1.0;
    int i = 0;
    while (i < n_primes) {
        if (n % primes[i] == 0)
            ans *= 1.0 - 1.0/primes[i]*1.0;
        while (n % primes[i] == 0)
            n /= primes[i];
        i++;
    }
    return ans*original;
}

int main() {
    
    ifstream fin ("input.txt");
    ofstream fout ("fractii.out");
    
    fin >> N;
    N = 10;
    
    // make a list of prime numbers.
    int c = 2;
    while ( c <= N ) {
        if (not_prime[c]) {
            c++; continue;
        }
        primes.push_back(c);
        n_primes++;
        for (int i = 2*c; i <= N; i+=c)
            not_prime[i] = true;
            
        c++;
    }
    
    long ans = 1;
    for (int i = 2; i <= N; i++)
        ans += 2 * totient(i);
    
    fout << ans << "\n";
    
    return 0;
}