Cod sursa(job #509114)

Utilizator padreatiAurelian Tutuianu padreati Data 10 decembrie 2010 14:47:41
Problema Fractii Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <cstdlib>
#include <fstream>
#include <iostream>


#include <algorithm>
#include <math.h>
#include <map>
#include <set>
#include <bitset>

using namespace std;

set<int> doPrimes(const int n) {
    set<int> p;
    bitset < 1000001 > bs;
    for (int i = 2; i <= n; i++) {
        if (!bs[i]) {
            bs[i] = true;
            int j = 2 * i;
            while (j < n + 1) {
                bs[j] = true;
                j += i;
            }
            p.insert(i);
            //            cout << i << " ";
        }
    }
    return p;
}

int totient(set<int> primes, int n) {
    bitset < 1000001 > bs;
    int m = n;
    set<int>::iterator it = primes.begin();
    int t = 1;
    while (it != primes.end()) {
        if (*it > m) break;
        if (m % (*it) == 0) {
            if (!bs[*it]) {
                bs[*it] = true;
                t *= (*it) - 1;
            } else t *= (*it);
            m /= (*it);
            continue;
        }
        it++;
    }
    if (m != 1) {
        if (!bs[m]) t*=(m-1);
        else t*=m;
    }
    return t;
}

int main() {

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

    int n;
    in >> n;

    set<int> primes = doPrimes(n);
    int t = 1;
    for (int i = 2; i <= n; i++) {
        //    for (int i = 2; i <= 10; i++) {
        int tot = totient(primes, i);
        t += tot * 2;
//                cout << i << "->" << tot << " ," << t << endl;
    }

    cout << t;
    out << t;

    return 0;
}