Cod sursa(job #2097415)

Utilizator alextodoranTodoran Alexandru Raul alextodoran Data 31 decembrie 2017 12:41:36
Problema Fractii Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <fstream>
using namespace std;

long long n, rez1;

long long pwr (int a, int b)
{
    if(b == 0)
        return 1;
    else
    {
        int p = pwr(a, b / 2);
        if(b % 2 == 1)
        {
            return p * p * a;
        }
        else
            return p * p;
    }
}

int main()
{
    ifstream fin ("fractii.in");
    ofstream fout ("fractii.out");
    fin >> n;
    for(int i = 1; i <= n; i++)
    {
        int p = 2, k, x = i, rez = 1;
        while(x > 1)
        {
            k = 0;
            while(x % p == 0)
            {
                k++;
                x /= p;
            }
            if(k >= 1)
            {
                //fout << p << "^" << k << " -> ";
                rez *= (p - 1) * pwr(p, k - 1);
                //fout << (p - 1) * pwr(p, k - 1) << "\n";
            }
            p++;
            if(p * p > x)
                p = x;
        }
        rez1 += rez;
        //fout << "\n";
    }
    fout << rez1 * 2 - 1;
    return 0;
}