Cod sursa(job #2500225)

Utilizator radustn92Radu Stancu radustn92 Data 27 noiembrie 2019 15:18:59
Problema Fractii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <cstdio>
#include <bitset>
using namespace std;

const int NMAX = 1000505;
bitset<NMAX> notPrime;
int phi[NMAX], N;

void precomputeTotientFunction() {
	for (int i = 2; i < NMAX; i++) {
		phi[i] = i;
	}

	for (int i = 2; i < NMAX; i++) {
		if (!notPrime[i]) {
			for (int j = i; j < NMAX; j += i) {
				phi[j] = phi[j] / i * (i - 1);
				notPrime[j] = 1;
			}
		}
	}
}

int main() {
	freopen("fractii.in", "r", stdin);
	freopen("fractii.out", "w", stdout);

	precomputeTotientFunction();
	scanf("%d", &N);
	long long result = 0;
	for (int i = 2; i <= N; i++) {
		result += phi[i] * 2;
	}

	// add 1 / 1 once only
	printf("%lld\n", result + 1);
	return 0;
}