Cod sursa(job #509129)

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


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

using namespace std;

vector<int> doPrimes(const int n) {
    vector<int> v;
    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;
            }
            v.push_back(i);
            //            cout << i << " ";
        }
    }
    return v;
}

int totient(vector<int> primes, int n) {
    int m = n;
    int t = 1;
    for (unsigned int i = 0; i < primes.size(); i++) {
        if (primes[i] > m) break;
        bool has = false;
        while (m % primes[i] == 0) {
            if (!has) {
                has = true;
                t *= primes[i] - 1;
            } else t *= primes[i];
            m /= primes[i];
            continue;
        }
    }
    if (m != 1) {
        t *= m;
    }
    return t;
}

int main() {

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

    int n;
    in >> n;

    vector<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;
}