Pagini recente » Cod sursa (job #3132485) | Cod sursa (job #970175) | Cod sursa (job #1037220) | Cod sursa (job #2672900) | Cod sursa (job #3266736)
#include <bits/stdc++.h>
using namespace std;
ifstream f("fractii.in");
ofstream g("fractii.out");
int n;
void compute_totients(int n, vector<int>& phi) {
for (int i = 1; i <= n; ++i) {
phi[i] = i; // Initialize each phi[i] to i
}
for (int i = 2; i <= n; ++i) {
if (phi[i] == i) { // i is a prime number
for (int j = i; j <= n; j += i) {
phi[j] = phi[j] * (i - 1) / i; // Apply the sieve formula
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
f >> n;
// Initialize the vector to store Euler's Totient values
vector<int> phi(n + 1);
// Precompute the Euler's Totient function for all values from 1 to n
compute_totients(n, phi);
unsigned long long rasp = 1;
// Sum up 2 * Euler(i) for each i from 2 to n
for (int i = 2; i <= n; ++i) {
rasp += 2 * phi[i];
}
g << rasp;
return 0;
}