Cod sursa(job #2834243)

Utilizator in-the-loopElliot Anderson in-the-loop Data 16 ianuarie 2022 18:11:55
Problema Fractii Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <bits/stdc++.h>
//1/1 1/2 1/3/ 1/4 1/5 5/1 4/1 3/1 2/1 2/3 2/5 3/2 3/5 
#define LL long long
#define MAX_SQRT 31626

using namespace std;

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

void generare(int facts[])
{
    facts[0] = 0;
    bitset<MAX_SQRT + 1> ciur;
    ciur[0] = 1;
    ciur[1] = 1;
    for (int i = 3; i <= MAX_SQRT; i++)
	if (!ciur[i])
	{
	    facts[++facts[0]] = i;
	    for (int j = i * 2; j <= MAX_SQRT; j += i)
		ciur[j] = 1;
	}
}

LL calcPhi(int n, int facts[])
{
    LL phi = 1;
    if (n % 2 == 0)
    {
	n /= 2;
	while (n % 2 == 0)
	{
	    phi *= 2;
	    n /= 2;
	}
    }

    for (int i = 1; i <= facts[0] && facts[i] <= n; i++)
	if (n % facts[i] == 0)
	{
	    phi *= (facts[i] - 1);
	    n /= facts[i];
	    while (n % facts[i] == 0)
	    {
		phi *= facts[i];
		n /= facts[i];
	    }
	}

    if (n > 1)
	phi *= n - 1;
    
    return phi;
}

int main()
{
    int n, facts[MAX_SQRT + 1];
    generare(facts);
    fin >> n;

    LL contor = 1;
    for (int i = 2; i <= n; i++)
	contor += calcPhi(i, facts) * 2;

    fout << contor;
    fin.close();
    fout.close();
    return 0;
}