Cod sursa(job #508904)

Utilizator padreatiAurelian Tutuianu padreati Data 9 decembrie 2010 21:13:17
Problema Fractii Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <math.h>
#include <map>

using namespace std;

int totient(int n) {
    map<int, int> mp;
    int m = n;
    int max = sqrt(n) + 1;
    int i = 2;
    while (true) {
        if (i > m || i > max)
            break;
        if (m % i == 0) {
            if (!mp[i]) {
                mp[i] = 1;
            } else {
                mp[i]++;
            }
            m /= i;
            continue;
        }
        i++;
    }
    if (m != 1)
        if (!mp[m])
            mp[m] = 1;
        else
            mp[m]++;

    map<int, int>::iterator it;
    long long a = 1, b = 1;
    for (it = mp.begin(); it != mp.end(); it++) {
        a *= ((*it).first - 1);
        b *= ((*it).first);
//        cout << (*it).first << ",";
    }
    return (n * a) / b;

}

int main() {

    ifstream in("fractii.in");
    ofstream out("fractii.out");

    int n;
    in >> n;

    int t = 1;
    for (int i = 2; i <= n; i++) {
        int tot = totient(i);
        t += tot*2;
//        cout << "\n" << i << "=" << tot << endl;
    }

    cout << t;
    out << t;

    return 0;
}